功能描述:
- 将容器中的元素反转,以及将容器中的数据进行排序。
函数原型:
- reverse(); //翻转链表
- sort(); //链表排序
代码演示:
#include<iostream>#include<list>using namespace std;//list容器的反转和排序void printList(const list<int> &L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;}//降序排序bool mycompare(int a, int b) {return a > b;}//反转void test01() {list<int>L1;//默认构造L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);cout << "反转前: ";printList(L1);//反转L1.reverse();cout << "反转前: ";printList(L1);}//排序void test02() {list<int>L1;//默认构造L1.push_back(30);L1.push_back(40);L1.push_back(10);L1.push_back(20);cout << "排序前: ";printList(L1);//反转L1.sort();cout << "排序后: ";printList(L1);//降序排序L1.sort(mycompare);cout << "降序排序后: ";printList(L1);}int main() {test01();test02();return 0;}
运行结果:
