首页 > 其他 > 详细

一些可能很常用的函数介绍(持续更新)

时间:2018-11-06 21:21:52      阅读:221      评论:0      收藏:0      [点我收藏+]

一些次常用的函数介绍:

  • replace
replace(初始位置,结束位置,替换字符串);
  • find
(母字符串).find(子字符串,起始位置)

如果没有设置起始位置默认为从头开始

  • random_shuffle()
random_shuffle(起始位置,结束位置)

将数组打乱。

  • nth_element()
nth_element(起始位置,所求位置,结束位置)

数组下表从零开始,nth_element(a,a+k,a+n),表示要把第k大的数放到下标为k的位置上。
时间复杂度为O(N),比所求数小的数都在这个数前面,比所求数大的数都在这个数后面,但是不保证有序。
最大的应用价值为求中位数

  • set_union() 求并集
  • set_intersection() 求交集
#include<iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
int main()
{
    set<int>s1,s2,s3,s4;
    s1.insert(1);
    s1.insert(2);
    s2.insert(2);
    s2.insert(4);
    set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,s3.begin()));
    set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s4,s4.begin()));
    for(set<int>::iterator it=s3.begin();it!=s3.end();it++)
        cout<<*it<<" ";
    cout<<endl;
    for(set<int>::iterator it=s4.begin();it!=s4.end();it++)
        cout<<*it<<" ";
}

一些可能很常用的函数介绍(持续更新)

原文:https://www.cnblogs.com/fengxunling/p/9918011.html

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