1.vector
定义 :vector<int> a
插入 :a.push_back(1) a.insert(a.end(),1) a.insert(a.begin()+1,1)
删除 :a.pop_back() 弹出尾部元素 a.erase(a.begin());
删除了除尾部的元素后就不能用数组a[]的方式来访问了,必须用迭代器
排序 sort(a.begin(),a.end()); sort(a.begin(),a.end(),greater<int>());
2.list
定义 :list<int> l;
插入 :l.push_back(1) l.push_front(1) l.insert(it,k)
删除 :l.pop_back() l.pop_front() l.erase(it)
排序 :l.sort() l.sort(greater<int>())
访问用迭代器
3.stack
a.push(k) a.top() 返回堆顶元素 a.pop() 弹出堆顶元素 和top一起用
4.queue
a.push(k) a.front() 返回队头元素 a.pop() 弹出队头元素 和 front()一起用
5.priority_queue
priority_queue<Node> q 默认大根堆 定义<符号
priority_queue<int , vector<int> ,greater<int> > q; 定义小根堆 定义>符号
q.push(k) q.top() q.pop()
6.set
插入 :a.insert(k)
删除 :a.erase(k) a.erase(it)
a.count(k) 出现次数 find(k) 返回迭代器 lower_bound(k) 第一个大于等于k的it upper_bound 第一个大于k的it
7.map
插入 :a[1]=1 不能直接访问a[1]不然也会被视为一次插入 应该用count(1)或者find(1)
a.insert(pair<int,int>(5,6))
删除 :a.erase(it) a.erase(key)
迭代器访问map中的元素 用 it->first it->second
map<int,int> a;
a.insert(pair<int,int>(5,5));
a.insert(pair<int,int>(6,6));
a[7]=7;
for (map<int,int>::iterator it=a.begin();it!=a.end();++it){
cout<<it->first<<' '<<it->second<<endl;
}
8.pair
初始化 make_pair(1,1) pair<int,int> a(1,1)
使用 a.first a.second
9.->与.的区别
实体引用它里面的成员变量用.
指针引用它里面的成员变量用->
map<int,int> a;
a[1]=1;
map<int,int>::iterator it=a.begin();
cout<<(*it).first<<endl; //转变为实体后用.
cout<<it->first<<endl; //指针直接用->
10.数组或stl二分查找函数
int a[100]= {4,10,11,30,69,70,96,100};
int b=binary_search(a,a+8,4);//查找成功,返回1
int c=binary_search(a,a+8,40);//查找失败,返回0
int d=lower_bound(a,a+8,10)-a;//大于等于10的数字位置
cout<<d<<endl;
int e=lower_bound(a,a+8,101)-a;
cout<<e<<endl;
int f=upper_bound(a,a+8,10)-a;//大于10的数字位置
cout<<f<<endl;
int g=upper_bound(a,a+8,101)-a;
cout<<g<<endl;
int tot=unique(a,a+8)-a-1; // 去重后的元素个数