STL 常用数据结构的小结,包括vector、stack、queue、priority queue、map、set、heap等,久没写程序,整理以备忘,更详细的内容参考STL手册。
更多此博客内容,点击这里Noalgo的博客
1. 向量(vector)。头文件<vector>
1 void f() 2 { 3 vector<int> v; //空 4 vector<int> v1(10); //指定个数 5 vector<int> v2(10, -1); //指定个数,初始值 6 int x[3] = {1, 2, 3}; vector<int> v3(x, x+3); //数组初始化,[st, ed) 7 vector<int> v4(v3); //vector初始化 8 9 v.empty(); v.size(); //是否为空,元素个数 10 v.push_back(100); v.pop_back(); //插入 删除元素 11 for (int i = 0; i < v.size(); i++) cout << v[i] << endl; //数组遍历 12 for(vector<int>::iterator p = v.begin(); p != v.end(); p++) cout << *p << endl; //迭代器遍历 13 }
2. 栈(stack)。头文件<stack>。
1 void f() 2 { 3 stack<int> s; //初始化 4 s.push(100); s.pop(); //入栈 出栈 5 s.top(); s.empty(); s.size(); //取栈顶,判栈空,栈大小 6 while(!s.empty()) s.pop(); //清空栈 7 }
3. 队列(queue)。头文件<queue>。
1 void f() 2 { 3 queue<int> q; //初始化 4 q.push(100); q.pop(); //入队 出队 5 q.front(); q.back(); q.empty(); q.size(); //取队首 队尾 判队空 队大小 6 while(!q.empty()) q.pop(); //清空队列 7 }
4. 仿函数(functor)。头文件<functional>。部分操作用到了仿函数,做个简单介绍。仿函数是实现了operator()的类,可以模拟函数的行为,这里只涉及关系运算的仿函数。
1 struct cmp{ bool operator()(const int& a, const int& b) {return a > b;} }; 2 void f() 3 { 4 int a[5] = {1, 3, 2, 5, 4}; 5 sort(a, a+5); //从小到大 6 //STL自带关系仿函数 equal_to<T> not_equal_to<T> greater<T> greater_equal<T> less<T> less_equal<T> 7 sort(a, a+5, greater<int>()); //从大到小 8 //自定义关系仿函数 9 sort(a, a+5, cmp()); //从大到小 10 }
5. 优先队列(prority queue)。头文件<queue>。
1 struct Node{ int x, y; Node(int a = 0, int b = 0):x(a), y(b){} }; //Node结构 2 bool operator<(Node a, Node b){return a.x > b.x;} //重载Node < 3 struct cmp{ bool operator()(const Node&a, const Node&b){return a.x > b.y;}}; //仿函数 4 void f() 5 { 6 priority_queue<int> q; //默认小于,大根堆,队首元素最大 7 priority_queue<int, vector<int>, greater<int> > q1; //大于,小根堆 8 priority_queue<Node> q2; //使用重载的<,大根堆 9 priority_queue<Node, vector<Node>, cmp> q3; //使用仿函数,大于,小根堆 10 11 q.push(100); q.pop(); //入队 出队 12 q.top(); q.empty(); q.size(); //取队首 判队空 队大小 13 while(!q.empty()) q.pop(); //清空队列 14 }
6. map。头文件<map>。
1 struct cmp{ bool operator()(const int& a, const int& b) {return a > b;}};//仿函数 2 void f() 3 { 4 map<int, int> m; //默认按key升序排 5 map<int, int, cmp> m1; //根据cmp按降序排 6 m[1] = 2; m.insert(pair<int,int>(1, 2)); m.insert(map<int,int>::value_type(1, 2));//3种插入方法 7 m.find(1); //查找,ans==m.end()时查找失败,使用ans->first,ans->second访问 8 m[1]; //访问元素(确定1存在) 9 m.empty(); m.clear(); m.erase(p); //判空 清空 删除迭代器p所指元素(确定存在) 10 }
7. 集合(set)。头文件<set>。
1 struct Node{ int x, y; Node(int a = 0, int b = 0):x(a), y(b){} }; //Node结构 2 struct cmp{ bool operator()(const Node&a, const Node&b){return a.x > b.y;}}; //仿函数 3 void f() 4 { 5 set<int> s; //默认从小到大排序 6 set<int, greater<int> > s1; //从大到小排序 7 set<int> s2(s); set<int> s3(st, ed); //指定内容set初始化 8 set<Node, cmp> s4; //自定义结构体,排序函数 9 10 s.insert(100); s.insert(p, 100); //插入(在迭代器p或之后插入),返回pair<iterator, bool>,表示位置,成功与否 11 s.find(100); //查找,没找到时 p==s.end() 12 s.erase(100); s.erase(p); s.erase(p1, p2); //删除 13 s.empty(); s.clear(); s.size(); s.count(100); //判空 清空 大小 统计 14 for(p = s.begin(); p != s.end(); p++) cout << *p << endl; //遍历 15 16 //集合运算 17 set<int> a1; a1.insert(1); a1.insert(2); 18 set<int> a2; a2.insert(2); a2.insert(3); 19 vector<int> rs; vector<int>::iterator pEnd; 20 //交 21 rs.resize(a1.size() + a2.size()); 22 pEnd = set_intersection(a1.begin(), a1.end(), a2.begin(), a2.end(), rs.begin()); 23 rs.resize(pEnd - rs.begin()); 24 //并 25 rs.resize(a1.size() + a2.size()); 26 pEnd = set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), rs.begin()); 27 rs.resize(pEnd - rs.begin()); 28 //差 A-B:属于A且不属于B 29 rs.resize(a1.size() + a2.size()); 30 pEnd = set_difference(a1.begin(), a1.end(), a2.begin(), a2.end(), rs.begin()); 31 rs.resize(pEnd - rs.begin()); 32 //对称差 (A-B)U(B-A):属于A或B但不同时属于A与B 33 rs.resize(a1.size() + a2.size()); 34 pEnd = set_symmetric_difference(a1.begin(), a1.end(), a2.begin(), a2.end(), rs.begin()); 35 rs.resize(pEnd - rs.begin()); 36 }
9. 堆(heap)。头文件<algorithm>。
1 struct cmp{ bool operator()(const int& a, const int& b){ return a > b; } }; 2 void print(vector<int> v){ for (int i= 0; i < v.size(); i++) cout << v[i] << " ";cout<<endl; } 3 int main () 4 { 5 vector<int> v; 6 v.push_back(4); v.push_back(1); v.push_back(7); print(v); 7 make_heap(v.begin(), v.end(), cmp()); print(v);//默认<,大根堆;使用仿函数,小根堆 8 v.push_back(2); push_heap(v.begin(), v.end(), cmp()); print(v);//先加数据,再push 9 pop_heap(v.begin(), v.end(), cmp()); v.pop_back(); print(v);//先pop,再删数据 10 sort_heap(v.begin(), v.end(), cmp()); print(v);//堆排,不再是堆 11 }
posted on
浙公网安备 33010602011771号