C++ Queue Library - queue() Function



Description

The C++ move constructor std::queue::queue() constructs the container with the contents of other using move semantics.

Declaration

Following is the declaration for move constructor std::queue::queue() form std::queue header.

C++11

queue( queue&& other);

Parameters

other − Another queue object of same type.

Return value

Constructor never returns values

Exceptions

This member function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::queue::queue() function.

#include <iostream>
#include <queue>
#include <list>

using namespace std;

int main(void) {
   auto it = {1, 2, 3, 4, 5};
   queue<int> q1(it);
   queue<int>q2(move(q1));

   cout << "Contents of q1 after move operation" << endl;
   while (!q1.empty()) {
      cout << q1.front() << endl;
      q1.pop();
   }
   
   cout << endl << endl;

   cout << "Contents of q2 after move operation" << endl;
   while (!q2.empty()) {
      cout << q2.front() << endl;
      q2.pop();
   }

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Contents of q1 after move operation
Contents of q2 after move operation
1
2
3
4
5
queue.htm
Advertisements