C ++中的链排序
在本节中,我们将看到如何使用C++标准库对数组或链表进行排序。在C++中,有多个不同的库可用于不同的目的。排序是其中之一。
C++函数std::list::sort()按升序对列表中的元素进行排序。相等元素的顺序被保留。它使用operator<进行比较。
示例
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 4, 2, 5, 3};
cout << "Contents of list before sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
l.sort();
cout << "Contents of list after sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
return 0;
}输出结果
Contents of list before sort operation 1 4 2 5 3 Contents of list after sort operation 1 2 3 4 5
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短