如何对整数对列表进行排序
How do I sort a list of pairs of ints?
>我正在尝试将数据作为整数对列表输入,按第一个对值排序,然后按第二个值对进行排序,然后输出排序对值;
#include <list>
#include <iostream>
#include <algorithm>
//experiment with list sort of pair
using namespace std;
int main (int argc, char* argv[]){
int N,x,y;
list< pair<int,int> >::iterator it;
list< pair<int,int> > a;
cout<<" number of pairs?"<<endl;
cin >> N;
cout<<"enter pairs, with space between 1st and second of pair"<<endl;
for(int i=0;i<N;++i) {
cin >> x >> y;
a.push_back(make_pair(x,y));
}
cout<<"sorted pairs;"<<endl;
sort(a.begin(),a.end()); // Sorts first the x, then the y-coordinate
for (it=a.begin(); it!=a.end(); ++it) {
cout << " " << (*it).first << " " << (*it).second << endl;
}
return 0;
}
std::sort
,来自 <algorithm>
标头,仅适用于随机访问迭代器,而 std::list
没有。 但是std::list
确实有自己的排序成员函数,它使用不同的算法。
a.sort();
相关文章:
- C++ 如何比较n个排序的整数向量以找到互元素?
- 有没有更好的方法对C++中的三个整数进行排序?
- Constexpr 可变参数模板,用于对无符号整数进行重新排序
- 如何在一个巨大的文本文件中对整数进行排序
- 根据偶数值对整数向量进行排序
- C++一个具有 2 个参数(___ _____,整数长度)的函数中的快速排序
- 对包含整数的文本文件进行排序时,必须逐行进行排序
- 排序函数如何在整数对的向量上工作?
- 输出可序列化的排序整数
- 在 C++ 中使用 10 个随机整数的数组进行气泡排序不起作用
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- QuickSort无法始终如一地对100 000个整数的数据集进行排序
- 整数阵列合并排序比QuickSort快
- 计算以毫秒为单位的分类算法对整数阵列排序的时间
- 如何输入整数并将其从最高到最低排序
- 对包含整数和字符的字符串数组进行排序
- 将整数分配给 std::vector 中排序的唯一元素
- 从文本文件将整数读入简单的链表中.然后对整数列表进行气泡排序并读出到另一个文件
- c++快速排序算法中500000排序整数数组的Seg错误
- 如何使用现有的整数排序来排序整数元组