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