The C++ std::queue::operator=() function is used to copy elements from one queue to another queue maintaining the proper order. when this function is invoked, it deallocates memory associated with the target queue and then copies the elements from the source queue. This function ensures that both queue are independent after assignment, preventing the modification of one queue affecting the other. The time complexity of this function is linear O(n).
SyntaxFollowing is the syntax for std::queue::operator=() function.
operator=( const queue<T,Container>& other ) or operator=( queue<T,Container>&& other );Parameters
This function returns this pointer.
ExampleLet's look at the following example, where we are going to assign one queue to another queue.
#include <iostream> #include <queue> int main() { std::queue<int> x, y; x.push(1); x.push(22); y = x; while (!y.empty()) { std::cout << y.front() << " "; y.pop(); } return 0; }Output
Output of the above code is as follows −
1 22Example
Consider the another scenario, where we are going to assign the empty queue to another queue.
#include <iostream> #include <queue> int main() { std::queue<int> queue1, queue2; queue2 = queue1; std::cout << " Size after assigning:" << queue2.size() << std::endl; return 0; }Output
Following is the output of the above code −
Size after assigning:0Example
In the following example, we are going to assign the queue to itself and observing the output.
#include <iostream> #include <queue> int main() { std::queue<int> x; x.push(1); x.push(2); x = x; std::cout << "Size after assigning: " << x.size() << std::endl; return 0; }Output
If we run the above code it will generate the following output −
Size after assigning: 2
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