常用STL速查手册
#include<string>:
常用操作:
1 2 3 4 5 6 7 8 9 10 11 12 13
| string s; s.begin(); s.end(); scanf("%s",s.c_str()); s[i] == s.at(i); int indxe = s.find(char ch); s.size(); s.append(string post); s.pop_back();
s.substr(int begin,int len); insert(iterator iter,type val); erase(iterator iter);
|
#include<pair>
常用操作
1 2 3 4
| typedef pair<int,char> PII; PII a; a = make_pair(5,'f'); a.first;a.second;
|
#include<vector>:
vector的常用初始化操作:
1 2 3 4 5 6
| vector<int> v; vector<char> g(n,i); vector<char> f(g.begin() + [offset],g.end()); vector<int> c(v); v.assign (7,100);
|
vector的常用函数:
1 2 3 4 5 6 7 8 9 10 11
| clear(); begin(); end(); size(); empty(); front(); back(); push_back(member_type x); pop_back(); erase(iterator l[,iterator r]); v.at[i] == v[i]
|
#include<stack>:
常用操作:
1 2 3 4 5
| push(type x); pop() empty(); size(); top();
|
#include<queue>
常用操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
q.push(int x); q.pop(); q.empty(); q.size(); q.front();
q.back(); -------------------
heap.size(); heap.empty(); heap.push(int x); heap.pop(); heap.top();
|
#include<deque>
常用操作
1 2 3 4 5 6 7 8 9 10 11
|
q.empty(); q.size(); q.front(); q.back(); q.push_back(int x); q.push_front(int x); q.pop_back(); q.pop_front();
|
#include<map>
常用操作
1 2 3 4 5 6 7 8 9 10
|
dic.empty(); dic.size(); dic.insert({"hello",1}); dic.erase({"hello",1}); dic.find("hello"); dic.count("hello");
|
#include<set>
常用操作
1 2 3 4 5 6 7 8
|
set/multiset<int> s; s.insert(int x); s.find(int x); s.erase(int x/iterator);
|
#include<unordered_set/multiset/unordered_map/multimap>
常用操作
带有unordered意味着底层为hash,与上面的操作类似,但时间复杂度为O(1)
#include<algorithm>
常用函数
最大值,最小值,交换
1 2 3
| max(max(a,b),c); min(); swap(a,b);
|
快速排序,默认排序结果为升序;
1 2
| int num[] = {5,6,9,10,6,2}; sort(num,num + 6,cmp);
|
字符串反转
1 2 3
| string s = "hello wolrd"; reverse(s.begin(),s.end()); cout << s << endl;
|
去重
1 2 3 4 5 6 7 8 9
| int num[] = {10,20,20,20,30,30,20,20,10}; vector<int> v(num,num + 9); sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end()); for (auto a : v) cout << a << ' ';
|
全排列
1 2 3 4 5 6 7 8 9 10 11 12
|
int num[] = {1,2,3,4,5}; int cnt = 1; while (next_permutation(num,num + 5)) { cnt++; for (auto x : num) cout << x << ' '; cout << endl; } cout << cnt;
|
本内容仅用作STL简单使用字典
作者:自豪的澡巾QAQ
链接:https://www.acwing.com/blog/content/6096/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。