首页 > 编程语言 > 详细

STL算法

时间:2019-02-03 15:12:08      阅读:152      评论:0      收藏:0      [点我收藏+]

头文件:<algorithm>

max  较大值   min  较小值  O(1)

sort  快速排序   stable_sort  稳定快速排序  O(n log n)

  sort(iterator_begin,iterator_end);

  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));

STL算法

原文:https://www.cnblogs.com/Pedesis/p/10350332.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!