一些可能很常用的函數介紹(持續更新)
阿新 • • 發佈:2018-11-06
lac n) 常用 介紹 持續更新 復雜度 都在 tro section
一些次常用的函數介紹:
- 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<<" "; }
一些可能很常用的函數介紹(持續更新)