#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
void printList(list<int>& L) {
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
bool myCompare(int val1, int val2){
//降序 就让第一个数 > 第二个数
return val1 > val2;
}
//反转和排序
void test01(){
list<int> L;
L.push_back(90);
L.push_back(30);
L.push_back(20);
L.push_back(70);
printList(L);
//反转容器的元素
L.reverse();
printList(L);
//排序
//所有不支持随机访问迭代器的容器,不可以用标准算法
//不支持随机访问迭代器的容器,内部会提供对应的一些算法
//sort(L1.begin(),L1.end());
L.sort(); //默认的排序规则 从小到大
printList(L);
L.sort(myCompare); //指定规则,从大到小
printList(L);
}
int main() {
test01();
system("pause");
return 0;
}
list容器 -- 排序反转
最新推荐文章于 2024-04-21 08:32:26 发布