【发布时间】:2015-07-07 04:58:26
【问题描述】:
我有一个数组 A:
A = [10 11 3 15 8 7]
index = 0 1 2 3 4 5
我想对这个数组进行排序。排序后我想要旧索引的信息。为此我可以创建一个这样的结构。
struct VnI{
int value;
int index;
};
根据值对结构数组进行排序解决了我的问题。但我想知道是否可以使用 sort 或 C++11 中的任何其他函数来解决这个问题。
我试过这种方式:
struct VnI{
int V;
int I;
};
bool comparator(VnI x,VnI y){
if(x.V < y.V)
return true;
return false;
}
int maximumGap(const vector<int> &A) {
vector<VnI> B;
for(int i = 0;i < A.size();i++){
B[i].I = i;
B[i].V = A[i];
}
sort(B.begin(),B.end(),comparator);
for(int i = 0;i < B.size();i++){
cout<<B[i].I<<" "<<B[i].V<<endl;
}
}
但是我遇到了运行时错误。 请帮忙。
【问题讨论】:
标签: c++ arrays sorting c++11 structure