C++ 中的 Strand 排序
在本节中,我们将了解如何使用 C++ 的标准库对数组或链表进行排序。在 C++ 中,可以使用多个不同的库来实现不同的用途。排序就是其中之一。
C++ 函数 std::list::sort() 以升序方式对列表中的元素进行排序。相等元素的顺序保持不变。它使用运算符< 进行比较。
示例
#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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP