Overview
The standard library provides queue adapter to provide FIFO (First In First Out) operations, where elements are inserted into one end of the container and extracted from the other.
Details
queue is basically an adapter based on associative containers such as deque or list that provides FIFO operations such as push_back() and pop_front().
Syntax
The syntax is as below. Template parameter T represents the datatype to store and Container represents the container used for storage and operations. deque and list are possible candidates.
template < class T, class Container = deque<T> >class queue;
Members
It defines following types
Name | Description |
---|---|
value_type | The first template parameter (T) |
container_type | The second template parameter (Container) |
reference | value_type& |
const_reference | const value_type& |
size_type | an unsigned integral type |
Operation
stack can be graphically represented as below.
New elements are stored in the host container. Operations such as push and pop are supported where elements are added or removed from the top.
Complexity
The complexity of push and pop operations are O(1).
Functionality
Constructors
In following constructors use default allocator. Custom allocators can be used by passing them as an additional argument.
Name | Description |
---|---|
queue() | Default Constructor. The default container is deque. Example: //v:{} queue<int> v; //v2:{} queue<int,list<int>> v2; |
queue(const container_type& c) | Construct from container c. Note that the underlying container has to be same. Example:
|
queue(const queue& x) | copy constructor. Note that the underlying container has to be same. Example:
|
queue(queue&& x) | move constructor. |
Capacity
Name | Description |
---|---|
size_type size() | Returns the number of elements. Calls size() method of the underlying container. Example: //v:{1,2,3,4} queue<int> v(deque<int>{1,2,3,4});//prints 4 cout << v.size(); |
bool empty() | Test whether vector is empty. Calls empty() method of the container. |
Element Access
Name | Description |
---|---|
reference front() | Returns reference to the first element. Calls front() method of the underlying container.Example:
//v:{1,2,3,4} queue<int> v(deque<int>{1,2,3,4}); |
reference back() | Returns reference to the last element. Calls back() method of the underlying container.Example:
//v:{1,2,3,4} queue<int> v(deque<int>{1,2,3,4}); |
Modifiers
Name | Description |
---|---|
void push(const value_type& val) void push(value_type&& val) | Adds element at the back. Calls push_back() method of the underlying container. Example: queue<int> v{}; |
void pop() | Deletes the element at the front. Calls pop_front() method of the underlying container.Example:
|
void swap(queue& v) | Swap content with v. Note T has to be same. Example: queue<int> v(deque({1,2,3}); queue<int> v2(deque{4,5,6});
|
void emplace(Args ...arg) | Construct and insert element at the back using arg. Calls emplace_back() method of the underlying container. Example: queue<int> v(deque{1,2,3});
|
No comments:
Post a Comment