【发布时间】:2014-05-23 15:06:57
【问题描述】:
我想按向量的第二个元素对向量进行排序。 代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool compare(const pair<int,int>&i, const pair<int,int>&j){
return i.second < j.second;
}
int main()
{
vector<pair<int,int> >a(100);
// values for test
a[0].first=0; a[0].second=21;
a[1].first=1; a[1].second=100;
a[2].first=2; a[2].second=100;
a[3].first=3; a[3].second=30;
a[4].first=4; a[4].second=17;
sort(a.begin(),a.end(),compare);
for(int i=0;i<5;i++)
cout << a[i].second << " " << a[i].first << endl;
}
程序返回
0 0
0 0
0 0
0 0
0 0
我哪里做错了?
【问题讨论】: