STL容器适配器
2009-11-26 15:35
197 查看
STL提供了三个容器适配器:queue、priority_queue、stack。这些适配器都是包装了vector、list、deque中某个顺序容器的包装器。注意:适配器没有提供迭代器,也不能同时插入或删除多个元素。下面对各个适配器进行概括总结。
(1)stack用法
#include <stack>
template < typename T, typename Container=deque > class stack;
可以使用三个标准顺序容器vecotr、deque、list中的任何一个作为stack的底层模型。
bool stack<T>::empty() //判断堆栈是否为空
void stack<T>::pop() //弹出栈顶数据
stack<T>::push(T x) //压入一个数据
stack<T>::size_type stack<T>::size() //返回堆栈长度
T stack<T>::top() //得到栈顶数据
(2)queue用法
#include <queue>
template<typename T, typename Container = deque<T> > class queue;
第一个参数指定要在queue中存储的类型,第二个参数规定queue适配的底层容器,可供选择的容器只有dequeue和list。对大多数用途使用默认的dequeue。
queue<T>::push(T x)
void queue<T>::pop()
T queue<T>::back()
T queue<T>::front()
queue<T>::size_type queue<T>::size()
bool queue<T>::empty()
(3)priority_queue用法
#include <queue>
template <typename T, typename Container = vector<T>, typename Compare = less<T> > class priority_queue;
priority_queue也是一个队列,其元素按有序顺序排列。其不采用严格的FIFO顺序,给定时刻位于队头的元素正是有最高优先级的元素。如果两个元素有相同的优先级,那么它们在队列中的顺序就遵循FIFO语义。默认适配的底层容器是vector,也可以使用deque,list不能用,因为priority_queue要求能对元素随机访问以便进行排序。
priority_queue<T>::push(T x)
void priority_queue<T>::pop()
T priority_queue<T>::top()
priority_queue<T>::size_type priority_queue<T>::size()
bool priority_queue<T>::empty()
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系,用法有三:(下文转自http://www.cnblogs.com/vvilp/articles/1504436.html)
优先队列第一种用法,通过默认使用的<操作符可知在整数中元素大的优先级高。
priority_queue<int> qi;
示例中输出结果为:9 6 5 3 2
优先队列第二种用法,建立priority_queue时传入一个比较函数,使用functional.h函数对象作为比较函数。
priority_queue<int, vector<int>, greater<int> >qi2;
示例2中输出结果为:2 3 5 6 9
优先队列第三种用法,是自定义优先级。
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
priority_queue<node> qn;
在示例3中输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
在该结构中,value为值,priority为优先级。通过自定义operator<操作符来比较元素中的优先级。注意:必须是自定义<操作符才行,把上述的结构中的<操作符改成>编译不通过。
(1)stack用法
#include <stack>
template < typename T, typename Container=deque > class stack;
可以使用三个标准顺序容器vecotr、deque、list中的任何一个作为stack的底层模型。
bool stack<T>::empty() //判断堆栈是否为空
void stack<T>::pop() //弹出栈顶数据
stack<T>::push(T x) //压入一个数据
stack<T>::size_type stack<T>::size() //返回堆栈长度
T stack<T>::top() //得到栈顶数据
(2)queue用法
#include <queue>
template<typename T, typename Container = deque<T> > class queue;
第一个参数指定要在queue中存储的类型,第二个参数规定queue适配的底层容器,可供选择的容器只有dequeue和list。对大多数用途使用默认的dequeue。
queue<T>::push(T x)
void queue<T>::pop()
T queue<T>::back()
T queue<T>::front()
queue<T>::size_type queue<T>::size()
bool queue<T>::empty()
(3)priority_queue用法
#include <queue>
template <typename T, typename Container = vector<T>, typename Compare = less<T> > class priority_queue;
priority_queue也是一个队列,其元素按有序顺序排列。其不采用严格的FIFO顺序,给定时刻位于队头的元素正是有最高优先级的元素。如果两个元素有相同的优先级,那么它们在队列中的顺序就遵循FIFO语义。默认适配的底层容器是vector,也可以使用deque,list不能用,因为priority_queue要求能对元素随机访问以便进行排序。
priority_queue<T>::push(T x)
void priority_queue<T>::pop()
T priority_queue<T>::top()
priority_queue<T>::size_type priority_queue<T>::size()
bool priority_queue<T>::empty()
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系,用法有三:(下文转自http://www.cnblogs.com/vvilp/articles/1504436.html)
优先队列第一种用法,通过默认使用的<操作符可知在整数中元素大的优先级高。
priority_queue<int> qi;
示例中输出结果为:9 6 5 3 2
优先队列第二种用法,建立priority_queue时传入一个比较函数,使用functional.h函数对象作为比较函数。
priority_queue<int, vector<int>, greater<int> >qi2;
示例2中输出结果为:2 3 5 6 9
优先队列第三种用法,是自定义优先级。
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
priority_queue<node> qn;
在示例3中输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
在该结构中,value为值,priority为优先级。通过自定义operator<操作符来比较元素中的优先级。注意:必须是自定义<操作符才行,把上述的结构中的<操作符改成>编译不通过。
相关文章推荐
- stl 顺序容器适配器之stack, priority_queue, queue用法
- STL之容器适配器
- 从零开始学C++之STL(十一):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
- stl 容器适配器、迭代器适配器和函数适配器讲解
- stl 容器适配器、迭代器适配器和函数适配器讲解
- 初探STL之容器适配器
- STL 笔记(三) 容器适配器 stack、queue、priority_queue
- STL 容器简介:C++ 容器:顺序性容器、关联式容器和容器适配器
- C++ STL 容器适配器
- STL里的容器、迭代器、算法、适配器
- STL的6大组件:容器、类属算法、迭代器、函数对象、适配器、分配器。
- STL系列(9):容器适配器
- STL之顺序容器适配器(栈的链表实现代码)
- C++ STL 容器适配器
- STL 容器适配器
- STL容器适配器
- 【优先队列】【堆】STL之priority_queue、make_heap()、push_heap()、pop_heap()、容器适配器
- C++ STL 容器适配器
- STL容器适配器
- C++的STL容器之容器适配器:stack、queue、priority_queue