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

更新於: 30-Jul-2019

256 瀏覽

啟動你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.