Saturday, September 21, 2024

queue

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 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
NameDescription
value_typeThe first template parameter (T)
container_typeThe second template parameter (Container)
referencevalue_type&
const_referenceconst value_type&
size_typean 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.

NameDescription
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:

//v:{1,2,3,4}
queue<int> v(deque<int>{1,2,3,4});

//v2:{1,2,3,4}
queue<int,list<int>> v2(list<int>{1,2,3,4});
queue(const queue& x)copy constructor.  Note that the underlying container has to be same.

Example:

//v:{1,2,3,4}
queue<int,list<int>> v(list<int>{1,2,3,4});

//v2:{1,2,3,4}
queue<int,list<int>> v2(v);
queue(queue&& x)move constructor.

Capacity
NameDescription
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
NameDescription
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});

//prints 1. 
cout << v.front(); 
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});

//prints 4. 
cout << v.back(); 

Modifiers
NameDescription
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{};

//v:{1}
v.push(1);

//v:{1,2}
v.push(2);
void pop()Deletes the element at the front. Calls pop_front() method of the underlying container.

Example:

//v:{1,2}
queue<int> v(deque<int>{1,2});
//v:{2}
v.pop();
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});

//v2:{1,2,3}
//v:{4,5,6}
v2.swap(v);
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});

//v:{1,2,3,4}
v.emplace(4);





No comments:

Post a Comment