关于STL容器的简单总结
1、结构体中重载运算符的示例
- //结构体小于符号的重载
- struct buf {
- int a,b;
- bool operator < (const buf& c1) const { //注意:第二个const一定不能少
- return a<c1.a;
- }
- };
- //或
- struct foo {
- int a,b;
- };
- bool operator < (const foo &x, const foo &y)
- {return x.a < y.a;}
复制代码 11、二分查找
注:lower_bound和upper_bound 使用前提均为原序列有序!- #include <queue>
- queue<int>a; //定义
- a.push(x); //压入
- a.pop(); //弹出
- a.size(); //取大小
- a.front(); //访问队首元素
- a.back(); //访问队尾元素
- a.empty(); //判断队列是否为空
复制代码 12、reverse()
- #include <queue>
- priority_queue<int,vector<int>,greater<int> > c; //定义从小到大的int类型的优先队列
- priority_queue<int> c; //定义从大到小的int类型的优先队列
- c.push(); //压入
- c.pop(); //弹出队首元素
- c.top(); //访问队首元素
- c.empty(); //判断队列是否为空
- //如是结构体必须重载'<'
复制代码 13、bitset
- #include <deque>
- deque <int> b; //定义双端队列
- b.push_front(x); //在队首压入元素
- b.push_back(x); //在队尾压入元素
- b.pop_front(); //弹出队首元素
- b.pop_back(); //弹出队尾元素
- b.size(); //取大小
- b.back(); //访问队尾元素
- b.front(); //访问队首元素
- b.empty(); //判断队列是否为空
- //可用[]访问
复制代码 14、__builtin_系列
- #include <stack>
- stack<int> d; //定义
- d.push(x); //压入元素
- d.pop(); //弹出栈顶元素
- d.top(); //访问栈顶元素
- d.size(); //取大小
- d.empty(); //判断栈是否为空
复制代码 转自http://ybt.ssoier.cn:8087
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |