-1
以前也有类似的问题,但我在应用这个概念时遇到了困难。我想跟踪我的向量对中的对的索引,然后,一旦排序,std :: cout输入顺序。分类时跟踪向量对索引
例如
cin >> 5 6 1 5 1 2 1 2 3 5
cout << 1 2 1 2 1 5 3 5 5 6
cout >> 3 4 2 5 1 //and this is where I am having trouble.
我想转换为三元组,其中一个元素是索引,但我无法得到符合我的需求。
任何帮助,将不胜感激。谢谢。
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
using namespace std;
bool compare(const pair<int,int>&A, const pair<int,int>&B);
int main()
{
vector<pair<int,int>> v;
pair<int,int> tok;
while(cin >> tok.first>>tok.second){v.push_back(tok);}
sort(v.begin(),v.end(), compare);
for(int i = (signed int)v.size()-1; i >= 0 ; i--)
{
cout << v.at(i).first << " ";
cout << v.at(i).second << " ";
}
cout << endl;
return 0;
}
bool compare(const pair<int,int>&A, const pair<int,int>&B)
{
return A.first > B.first;
}
对不起,我应该已经表明,这是一个作业的一部分。 –