std::list::sort
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
void sort(); |
(1) | |
template< class Compare > void sort( Compare comp ); |
(2) | |
按升序排列的元素进行排序。保证保留相等元素的顺序。 operator<的第一个版本使用比较的元素,第二个版本使用给定的比较函数
comp
。等同元件的顺序被保留.原文:
Sorts the elements in ascending order. The order of equal elements is guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function
comp
. The ordering of equivalent elements is preserved.目录 |
[编辑] 参数
comp | - | comparison function which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. |
[编辑] 返回值
(无)
[编辑] 为例
#include <iostream> #include <functional> #include <list> std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list) { for (auto &i : list) { ostr << " " << i; } return ostr; } int main() { std::list<int> list = { 8,7,5,9,0,1,3,2,6,4 }; std::cout << "before: " << list << "\n"; list.sort(); std::cout << "ascending: " << list << "\n"; list.sort(std::greater<int>()); std::cout << "descending: " << list << "\n"; }
输出:
before: 8 7 5 9 0 1 3 2 6 4 ascending: 0 1 2 3 4 5 6 7 8 9 descending: 9 8 7 6 5 4 3 2 1 0
[编辑] 复杂性
N · log(N)比较大小的容器,其中
N
.原文:
N · log(N) comparisons, where
N
is the size of the container.