The C++ std::queue::queue() function constructor initializes a queue, a first-in, first-out (FIFO) data structure. The default constructor queue() creates an empty queue and we can initialize a queue with elements from another container using queue(const Container & cont).
copy constructors queue and move constructors queue allow us to create a new queue from an existing one, either by copying or moving its elements.
SyntaxFollowing is the syntax for std::queue::queue() constructor.
explicit queue (const container_type& ctnr = container_type()); or explicit queue (const container_type& ctnr); or queue( queue&& other); or queue( queue& other );Parameters
The constructor never returns value.
ExampleLet's look at the following example, where we are going to use the default constructor.
#include <iostream> #include <queue> int main() { std::queue<int> a; std::cout << "Size of the queue after default construction: " << a.size() << std::endl; return 0; }Output
Following is the output of the above code −
Size of the queue after default construction: 0Example
Consider the another scenario, where we are going to use the copy constructor.
#include <iostream> #include <queue> int main() { std::queue<int> a; a.push(11); a.push(2); std::queue<int> b(a); std::cout << "Size of the queue after copy construction: " << b.size() << std::endl; return 0; }Output
Let us compile and run the above program, this will produce the following result −
Size of the queue after copy construction: 2Example
Following is the example, where we are going to use the move constructor.
#include <iostream> #include <queue> int main() { std::queue<int> a; a.push(111); a.push(22); std::queue<int> b(std::move(a)); std::cout << "Size of the queue after move construction: " << b.size() << std::endl; std::cout << "Size of the queue after move construction: " << a.size() << std::endl; return 0; }Output
Output of the above code is as follows −
Size of the queue after move construction: 2 Size of the queue after move construction: 0Example
In the following example, we are going to construct a queue using the different container.
#include <iostream> #include <queue> #include <deque> int main() { std::deque<int> a = {11,22,3,4}; std::queue<int, std::deque<int>> b(a); std::cout << "Queue size: " << b.size() << std::endl; return 0; }Output
If we run the above code it will generate the following output −
Queue size: 4
queue.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4