STL算法
头文件:<algorithm>
sort 快速排序 stable_sort 稳定快速排序 O(n log n)
sort(iterator_begin, iterator_end); //iterator_指迭代器位置 sort(iterator_begin, iterator_end, cmp); //cmp为自定义排序
reverse 翻转 O(n)
reverse(iterator_begin, iterator_end);
unique 去重(返回新范围尾部迭代器) O(n)
int len = unique(iterator_begin, iterator_end) - iterator_begin; //得到新范围长度
lower_bound/upper_bound 二分查找 O(log n)
int pos1 = lower_bound(iterator_begin, iterator_end, x) - iterator_begin; //第一个大于等于x的元素位置 int pos2 = upper_bound(iterator_begin, iterator_end, x) - iterator_begin; //第一个大于x的元素位置 int pos3 = lower_bound(iterator_begin, iterator_end, x, greater<int>()) - iterator_begin; //第一个小于等于x的元素位置 int pos4 = upper_bound(iterator_begin, iterator_end, x, greater<int>()) - iterator_begin; //第一个小于x的元素位置
next_permutation 下一个排列 O(n)
//字典序输出1~n的全排列 for (int i = 1; i <= n; i++) a[i] = i; do { for (int i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); } while(next_permutation(a + 1, a + n + 1));