C++ 使用向量进行排序
我正在写一个股票市场的程序,在这个程序中,我从一个文件中读取数据,并用符号和盈亏百分比进行排序。我已完成符号排序,但无法确定增益损失百分比。基本上我被教导使用向量。我们需要生成按百分比损益排序的列表,我需要按此组件对库存列表进行排序。但是,我不会按照组件的增益/损耗百分比对列表进行物理排序;而是提供与此组件相关的逻辑顺序。 所以基本上我添加了一个数据成员,一个向量,用来保存股票列表的指数,这些指数是按收益/损失百分比排序的。我称之为数组索引。因此,当我打印按增益/损耗百分比排序的列表时,我使用数组indexByGain来打印列表。我的问题是我需要如何开始的帮助,如果有人能给我举个例子或解释如何开始,我可以继续或纠正我的草稿,这将是有益的。下面是我的代码的草稿。stockType与文件中存储数据的位置有关C++ 使用向量进行排序,c++,C++,我正在写一个股票市场的程序,在这个程序中,我从一个文件中读取数据,并用符号和盈亏百分比进行排序。我已完成符号排序,但无法确定增益损失百分比。基本上我被教导使用向量。我们需要生成按百分比损益排序的列表,我需要按此组件对库存列表进行排序。但是,我不会按照组件的增益/损耗百分比对列表进行物理排序;而是提供与此组件相关的逻辑顺序。 所以基本上我添加了一个数据成员,一个向量,用来保存股票列表的指数,这些指数是按收益/损失百分比排序的。我称之为数组索引。因此,当我打印按增益/损耗百分比排序的列表时,我使用数
#include <iostream>
#include "stockType.h"
class stockListType
{
public:
void sortBySymbols();//sort out symbols and it comiples correctly.
void sortByGain();
void printByGain();
void insert(const stockType& item);
private:
vector<int> indexByGain;//declared a vector array indexByGain..
vector<stockType> list;
};
void stockListType::insert(const stockType& item)
{
list.push_back(item)//inserts the data from file to vector array.
}
//function prints out the gain
void stockListType::printByGain()
{
//my code to print out the gain..
}
//function to sort the gain and this is where i am stuck.
void stockListType::sortGain()
{
int i, j, min, maxindex;
for(i=0;i<list.size();i++)
{
min = i;
for(j=i+1;j<list.size();j++)
list[maxindex].getPercentage()<list[j].getPercentage();
maxindex = j;
indexGain.push_back(maxindex);
}
#包括
#包括“stockType.h”
类stockListType
{
公众:
void sortBySymbols();//对符号进行排序,它将正确显示。
void sortByGain();
void printByGain();
无效插入(常量库存类型和项目);
私人:
vector indexByGain;//声明了一个向量数组indexByGain。。
向量表;
};
void stockListType::insert(常量stockType和item)
{
list.push_back(item)//将数据从文件插入向量数组。
}
//函数输出增益
作废stockListType::printByGain()
{
//我的代码用来打印增益。。
}
//函数对增益进行排序,这就是我遇到的问题。
void stockListType::sortGain()
{
int i,j,min,maxindex;
对于(i=0;i初始化索引并使用std::sort:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
struct Data {
int value;
int percent;
};
typedef std::vector<Data> DataVector;
typedef DataVector::size_type size_type;
typedef std::vector<size_type> IndexVector;
DataVector data { { 1, 1 }, { 2, -2 }, { 3, 3 }, { 4, -4 }, { 5, 5} };
IndexVector index;
index.resize(data.size());
for(size_type i = 0; i < data.size(); ++i) {
index[i] = i;
}
struct Less
{
const DataVector& data;
Less(const DataVector& data)
: data(data)
{}
bool operator () (size_type a, size_type b) {
return data[a].percent < data[b].percent;
}
};
std::sort(index.begin(), index.end(), Less(data));
for(size_type i = 0; i < index.size(); ++i) {
std::cout << data[index[i]].value << ": " << data[index[i]].percent << std::endl;
}
}
#包括
#包括
#包括
int main()
{
结构数据{
int值;
整数百分比;
};
typedef std::向量数据向量;
typedef DataVector::size\u type size\u type;
typedef std::向量索引向量;
数据向量数据{{1,1},{2,-2},{3,3},{4,-4},{5,5};
指数向量指数;
index.resize(data.size());
对于(size_type i=0;i std::sort多次调用基元素的复制构造函数。如果性能下降,请检查你的复制构造函数。@cup什么是“基元素”?
std::sort(index.begin(), index.end(),
[&](size_type a, size_type b) { return data[a].percent < data[b].percent; }
);
for(auto i: index)
std::cout << data[i].value << ": " << data[i].percent << std::endl;