0
大家好我正在编写一个股票市场的程序,我从一个文件中读取并按照符号和百分比收益/损失进行排序。我已完成符号分类,但无法确定收益损失百分比。基本上我被指示使用矢量。我们被要求产生按百分比收益/损失排序的清单,我需要按这个组件排序清单。不过,我不是要按组件百分比收益/损失对物品进行实物分类;而是提供关于该组件的逻辑顺序。所以基本上我添加了一个数据成员,一个矢量来保存由组件百分比收益/损失排序的股票列表的指数。我把它称为数组indexByGain。所以当我打印按收益/损失百分比排序的列表时,我使用数组indexByGain打印列表。我的问题是如果有人能够帮助我解释如何解决这个问题,或者解释他们使用数组所做的示例代码将会有很大的帮助,我需要帮助。下面是一个示例代码排序百分比增益/损失:使用矢量排序百分比收益/损失
/* Name: stock_sort() */
/* Purpose: Sort stocks (stockListType) by percent gained or loss, ascending order */
/* Parameters: None */
/* Preconditions: list of stocks should be created and initialized as well as
sortIndicesGainLoss array */
/* Postconditions: sortIndicesGainLoss holds the indices of stockList sorted
by gain/loss */
void stockListType::stock_sort() {
int i, j;
int min;
int temp1, temp2;
for(i = 0; i < length; i++)
sortIndicesGainLoss[i] = i;
for(i = 0; i < length; i++)
{
min = i;
for(j = i + 1; j < length; ++j)
if(list[sortIndicesGainLoss[min]].percent_gain()>list[sortIndicesGainLoss[j]].percent_gain())
min = j;
temp1 = sortIndicesGainLoss[i];
sortIndicesGainLoss[i] = sortIndicesGainLoss[min];
sortIndicesGainLoss[min] = temp1;
}
}
你如何去使用载体。我真的很难尝试使用矢量来整理股票,因为我很困惑如何去做。如果有人能帮助我,我会感激。谢谢
不要用_开头写名字。此外,更喜欢超载比较运算符 – Manu343726
固定。或者,如果您的编译器支持C++ 11,则可以使用lambda表达式。然后最后一行看起来像'std :: sort(vector_var.begin(),vector_var.end(),[](STOCK&a,STOCK&b) - > bool {return(a.PercentGainLoss
Chen
我很抱歉,我是编程新手,不理解你做了什么。我们被指示使用向量数组来保存增益的索引。矢量 indexByGain –
Emy