目录
2. Non-member function overloads
2. Non-member function overloads
queue和priority_queue定义在头文件queue和命名空间std中。
queue
template <class T, class Container = deque<T> > class queue;
队列是一种容器适配器,专门设计用于在先进先出(FIFO)情况下操作,其中元素被插入容器的一端并从另一端提取。
队列被实现为容器适配器,它是使用特定容器类的封装对象作为其底层容器的类,提供一组特定的成员函数来访问其元素。元素被推入特定容器的"后面",并从其"前面"弹出。
底层容器可以是标准容器类模板中的一个,也可以是其他一些专门设计的容器类。这个底层容器应至少支持以下操作:
- empty
- size
- front
- back
- push_back
- pop_front
标准容器类deque和list满足这些要求。默认情况下,如果没有为特定的队列类实例化指定容器类,就会使用标准容器deque。
1. Member functions
1.1 constructor
// (1) initialization constructor
explicit queue(const container_type& ctnr);
// 构造一个容器适配器,其内部容器初始化为ctnr的拷贝
// (2) move-initialization constructor
explicit queue(container_type&& ctnr = container_type());
// 构造一个容器适配器,其内部容器通过移动构造来获取ctnr的值
// (3) allocator constructor
template <class Alloc> explicit queue(const Alloc& alloc);
// 构造一个容器适配器,其内部容器是用alloc作为参数构造的
// (4) initialization with allocator constructor
template <class Alloc> queue(const container_type& ctnr, const Alloc& alloc);
// 构造一个容器适配器,其内部容器是用cnr和alloc作为参数构造的
// (5) move-initialization with allocator constructor
template <class Alloc> queue(container_type&& ctnr, const Alloc& alloc);
// 构造一个容器适配器,其内部容器是用std::move(cnr)和alloc作为参数构造的
// (6) copy with allocator constructor
template <class Alloc> queue(const queue& x, const Alloc& alloc);
// 构造一个容器适配器,其内部容器是用x的内部容器作为第一个参数,alloc作为第二个参数构造的
// (7) move with allocator constructor
template <class Alloc> queue(queue&& x, const Alloc& alloc);
// 构造一个容器适配器,其内部容器是用移动x的内部容器作为第一个参数并传递alloc作为第二个参数构造的
1.2 empty
bool empty() const;
// 检测queue是否为空,是返回true,否则返回false
1.3 size
size_type size() const;
// 返回queue中元素的个数
1.4 front
reference& front();
const_reference& front() const;
// 返回队头元素的引用
1.5 back
reference& back();
const_reference& back() const;
// 返回队尾元素的引用
1.6 push
void push(const value_type& val);
void push(value_type&& val);
// 入队
1.7 emplace
template <class... Args> void emplace(Args&&... args);
// 对应push,区别是:
// 当调用push时,我们将元素类型的对象传递给它们,这些对象被拷贝到容器中
// 当调用emplace时,则是将参数传递给元素类型的构造函数,然后使用这些参数在容器管理的内存空间中直接构造元素
1.8 pop
void pop();
// 出队
1.9 swap
void swap(queue& x) noexcept;
// 交换
2. Non-member function overloads
2.1 relational operators
// (1)
template <class T, class Container> bool operator==(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
// (2)
template <class T, class Container> bool operator!=(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
// (3)
template <class T, class Container> bool operator<(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
// (4)
template <class T, class Container> bool operator<=(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
// (5)
template <class T, class Container> bool operator>(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
// (6)
template <class T, class Container> bool operator>=(const queue<T, Container>& lhs, const queue<T, Container>& rhs);
2.2 swap
template <class T, class Container>
void swap(queue<T, Container>& x, queue<T, Container>& y) noexcept(noexcept(x.swap(y)));
priority_queue
template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;
优先级队列是一种容器适配器,专门设计成它的第一个元素总是它所包含的元素中最大的一个,根据一些严格的弱排序标准。
这种情况类似于一个堆,其中的元素可以在任何时候被插入,并且只有最大的堆元素可以被检索(在优先级队列中处于最顶端的元素)。
优先级队列被实现为容器适配器,它是使用特定容器类的封装对象作为其底层容器的类,提供一组特定的成员函数来访问其元素。元素从特定容器的"后面"弹出,这就是所谓的优先级队列的顶部。
底层容器可以是任何一个标准的容器类模板或其他一些专门设计的容器类。容器应通过随机访问迭代器访问,并支持以下操作:
- empty
- size
- front
- push_back
- pop_back
标准的容器类vector和deque满足这些要求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,就会使用标准的容器vector。
支持随机访问的迭代器是需要在内部一直保持一个堆结构。这由容器适配器自动完成,在需要时自动调用算法函数make_heap、push_heap和pop_heap。
1. Member functions
1.1 constructor
// (1) initialization constructor
priority_queue(const Compare& comp, const Container& ctnr);
// 底层容器是ctnr的拷贝,按make_heap排序
// (2) range initialization constructor
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Compare& comp, const Container& ctnr);
// 底层容器是ctnr的拷贝,插入[first,last)范围内的元素,按make_heap排序
// (3) move-initialization constructor
explicit priority_queue(const Compare& comp = Compare(), Container&& ctnr = Container());
// 底层容器通过移动构造来获取ctnr的值,按make_heap排序
// (4) move-range initialization constructor
template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Compare& comp, Container&& ctnr = Container());
// 底层容器通过移动构造获取ctnr的值,插入[first,last)范围内的元素,按make_heap排序
// (5) allocator constructors
template <class Alloc> explicit priority_queue(const Alloc& alloc);
template <class Alloc> priority_queue(const Compare& comp, const Alloc& alloc);
template <class Alloc> priority_queue(const Compare& comp, const Container& ctnr, const Alloc& alloc);
template <class Alloc> priority_queue(const Compare& comp, Container&& ctnr, const Alloc& alloc);
template <class Alloc> priority_queue(const priority_queue& x, const Alloc& alloc);
template <class Alloc> priority_queue(priority_queue&& x, const Alloc& alloc);
// 使用特定的分配器值构造一个priority_queue
创建元素为链表节点的小根堆:
struct ListNode
{
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
struct cmp
{
bool operator()(const ListNode* n1, const ListNode* n2)
{
return n1->val > n2->val;
}
};
priority_queue<ListNode*, vector<ListNode*>, cmp> heap;
1.2 empty
bool empty() const;
// 检测stack是否为空,是返回true,否则返回false
1.3 size
size_type size() const;
// 返回stack中元素的个数
1.4 top
const_reference top() const;
// 返回堆顶元素的引用
1.5 push
void push(const value_type& val);
void push(value_type&& val);
// 入堆
1.6 emplace
template <class... Args> void emplace(Args&&... args);
// 对应push,区别是:
// 当调用push时,我们将元素类型的对象传递给它们,这些对象被拷贝到容器中
// 当调用emplace时,则是将参数传递给元素类型的构造函数,然后使用这些参数在容器管理的内存空间中直接构造元素
1.7 pop
void pop();
// 出堆
1.8 swap
void swap(priority_queue& x) noexcept;
// 交换
2. Non-member function overloads
2.1 swap
template <class T, class Container, class Compare>
void swap(priority_queue<T, Container, Compare>& x, priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));