C++ std::queue

std::queue

template <class T, class Container = deque<T> > class queue;

FIFO queue

queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated(封装) object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:

  • empty
  • size
  • front
  • back
  • push_back
  • pop_front

The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.

Template parameters
  • T: Type of the elements. Aliased as member type queue::value_type.
  • Container: Type of the internal underlying container object where the elements are stored. Its value_type shall be T. Aliased as member type queue::container_type.
Member types
member typedefinition
value_typeThe first template parameter (T)
container_typeThe second template parameter (Container)
size_typean unsigned integral type
Member functions
  • (constructor): Construct queue (public member function )
  • empty: Test whether container is empty (public member function )
  • size: Return size (public member function )
  • front: Access next element (public member function )
  • back: Access last element (public member function )
  • push: Insert element (public member function )
  • emplace: Construct and insert element (public member function )
  • pop: Remove next element (public member function )
  • swap: Swap contents (public member function )
Non-member function overloads
  • relational operators: Relational operators for queue (function )
  • swap (queue): Exchange contents of queues (public member function )
Non-member class specializations
  • uses_allocator: Uses allocator for queue (class template )
Code Example
#include <iostream>
#include <deque>
#include <list>
#include <queue>

using namespace std;

int main(int argc, char **argv)
{
    deque<int> deck (3, 100);
    list<int> list (2, 200);

    queue<int> first;
    queue<int> first1( deck ); ///< queue initialized to copy of deque

    /** empty queue with list as underlying container */
    queue<int, list<int> > first2;

    /**  */
    queue<int, list<int> > first3 ( list );
    return 0;
}
Reference

cplusplus


转载于:https://www.cnblogs.com/zi-xing/p/6246769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值