1.vector
#include<vector>
vector<int>a;//创建一个大小为空的对象a
vector<int>a(n)//创建一个大小为n的对象a
vector<vector<int> >a(n,vector<int>(m))//创建一个大小为n,m的二维数组,相当于a[n][m]
a.push_back()//向容器末尾加一个新元素 O(1)
a.pop_back()//删除容器末尾元素 O(1)
a.front()//返回容器第一个元素的引用 O(1)
a.back()//返回容器最后一个元素的引用 O(1)
a.begin()//返回容器第一个元素的迭代器(迭代器可通过*进行引用)例如*a.begin()==a.front()
a.end()//返回最后一个元素后面一位的迭代器
sort(a.begin(),a.end())//默认从小到大排序
sort(a.begin(),a.end(),comp)//根据comp函数排序
int pos = lower_bound(a.begin(), a.end(), k) - a.begin();//即返回值>=k的第一个元素的迭代器,
if(a[pos] == k) cout << a[pos] << '\n';
else cout << "find false\n";
a.upper_bound(k)//即返回值>k的第一个元素的迭代器
a.size()//获取a的元素个数
a.clear()//清空当前容器
2.deque
#include<deque>
deque<int>a;
a.push_front()//(将元素插到最前面 O(1)
a.pop_front()//删除最前面元素 O(1)
a.push_back()//向容器末尾加一个新元素 O(1)
a.pop_back()//删除容器末尾元素 O(1)
a.front()//返回容器第一个元素的引用 O(1)
a.back()//返回容器最后一个元素的引用 O(1)
a.begin()//返回容器第一个元素的迭代器(迭代器可通过*进行引用)例如*(a.begin()==a.front())
a.end()//返回最后一个元素后面一位的迭代器
sort(a.begin(),a.end())//默认从小到大排序
sort(a.begin(),a.end(),comp)//根据comp函数排序
int pos = lower_bound(a.begin(), a.end(), k) - a.begin();//即返回值>=k的第一个元素的迭代器,
if(a[pos] == k) cout << a[pos] << '\n';
else cout << "find false\n";
a.upper_bound(k)//即返回值>k的第一个元素的迭代器
a.size()//获取a的元素个数
a.clear()//清空当前容器
3.list
#include<list>
list<int>a; //创建空list
list<int>a(n); //创建含有n个元素的list
a.sort() //给list排序
a.sort(cmp)//根据comp函数排序
a.unique();//给list去重,前提是必须有序,可以用sort先排序后再用
a.merge(b) //合并两个list,默认里面的元素升序,b会成空list
a.begin() //返回指向第一个元素的迭代器
a.end() //返回末尾的迭代器
a.clear() //删除所有元素
a.empty() //如果list是空的则返回true
a.erase() 删除一个元素
例如
auto it = find(a.begin(), a.end(), 4); //删除list中元素值是4的一个元素
a.erase(it);
a.front() //返回第一个元素
a.back() //返回最后一个元素
a.pop_back()// 删除最后一个元素
a.pop_front() //删除第一个元素
a.push_back() //在list的末尾添加一个元素
a.push_front()// 在list的头部添加一个元素
a.remove() //删除指定位置上的一个元素
a.reverse() //把list的元素倒转
a.size() //返回list中的元素个数
4.set,multiset,unordered_set
multiset(可重复)和set有序,unordred_set无序
#include<set>
#include<unordered_set>
set<int>s;
set<int>s1 = {1, 3, 2, 4, 5, 6};
multiset<int>mus;
multiset<int>mus1 = {1, 3, 2, 4, 5, 6};
unordered_set<int>us;
unordered_set<int>us1 = {1, 3, 2, 4, 5, 6};
insert()//插入元素
count()//判断容器中是否存在某个元素
size()//返回容器的尺寸,也可以元素的个数
erase()//删除集合中某个元素, multiset是删除所有这个元素
clear()//清空集合
empty()//判断是否为空
begin()//返回第一个节点的迭代器
end()//返回最后一个节点加1的迭代器
lower_bound() //即返回值>=k的第一个元素的迭代器,
auto it = s.lower_bound(k);
if(it != s.end()) cout << *it;
upper_bound() //即返回值>k的第一个元素的迭代器
5.map,unordred_map,multimap
multimap(可重复)和map有序,unordred_map无序
#include<map>
#include<unordered_map>
multimap<int,int>mtmp;
unordered_map<int,int>ump;
map<int,int>mp;
insert()//插入容器,multimap不能下标访问,需要通过这个
for(int i = 1; i <= 12; i ++ ) ntmp.insert({i, i + 1});
count()//判断容器中是否存在某个元素
size()//返回容器的尺寸,也可以元素的个数
erase()//删除容器中某个元素, multimap是删除所有键是这个的元素
clear()//清空容器
empty()//判断是否为空
begin()//返回第一个节点的迭代器
end()//返回最后一个节点加1的迭代器
lower_bound() //即返回值>=k的第一个元素的迭代器,
auto it = mp.lower_bound(k);
if(it != mp.end()) cout << it->first << " " << it->second << '\n';
upper_bound() //即返回值>k的第一个元素的迭代器
6.stack
#include<stack>
stack<int> a;
int x;
a.push(x); //将x压入栈顶
a.top(); //返回栈顶的元素
a.pop(); //删除栈顶的元素
a.size(); //返回栈中元素的个数
a.empty(); //检查栈是否为空,若为空返回true,否则返回false
7.queue
#include<queue>
queue<int> a;
int x;
a.push(x); //将x入队
a.front(); //返回队首的元素
a.back();//返回队尾元素
a.pop(); //队首元素出队
a.size(); //返回队中元素的个数
a.empty(); //检查队是否为空,若为空返回true,否则返回false
8.priority_queue
#include<queue>
priority_queue<int>a; //降序,大根堆
priority_queue<int,vector<int>,greater<int>>b;//升序,小根堆
for(int i = 1; i <= 12; i ++ ) a.push(i);
for(int i = 1; i <= 12; i ++ ) b.push(i);
while(!a.empty()) cout << a.top() << " ",a.pop();cout << '\n';
while(!b.empty()) cout << b.top() << " ",b.pop();cout << '\n';
12 11 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 11 12
a.push(x); //将x入队
a.top(); //返回队首的元素
a.back();//返回队尾元素
a.pop(); //队首元素出队
a.size(); //返回队中元素的个数
a.empty(); //检查队是否为空,若为空返回true,否则返回false