C++ Queue Library - operator= Function



Description

The C++ function std::queue::operator= assign new contents to the queue by replacing old ones and modifies size if necessary.

Declaration

Following is the declaration for std::queue::operator= function form std::queue header.

C++98

queue<T, Container>& 
operator=( const queue<T,Container>& other )

Parameters

other − Another queue object of same type.

Return value

Returns this pointer.

Exceptions

This function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::queue::operator= function.

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

using namespace std;

int main(void) {
   auto it1 = {1, 2, 3, 4, 5};
   auto it2 = {10, 20};
   queue<int> q1(it1);
   queue<int> q2(it2);

   q2 = q1;

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

   cout << "Contents of q2" << 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
1
2
3
4
5
Contents of q2
1
2
3
4
5
queue.htm
Advertisements