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

一些次常用的函数介绍:

  • 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<<" ";
}

猜你喜欢

转载自www.cnblogs.com/fengxunling/p/9918011.html