![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Queue::operator==() Function
The C++ std::queue::operator==() function is used to check if both the queues are equal by comparing their elements and order. It returns true if both queues contain same elements otherwise it returns false. It is useful for comparing queues efficiently, providing a convenient way to check for equality without iterating through each element. The time complexity of this function is linear i.e.O(n).
Syntax
Following is the syntax for std::queue::operator==() function.
bool operator== (const queue<T,Container>& q1, const queue<T,Container>& q2);
Parameters
- q1 − It indicates the first queue object.
- q2 − It indicates the second queue object.
Return value
This function returns true if both queues are identical otherwise false.
Example
Let's look at the following example, where we are going to initialize the both queue with same elements and applying the operator==() function.
#include <iostream> #include <queue> int main() { std::queue<int> a, b; a.push(11); a.push(222); b.push(11); b.push(222); if (a == b) std::cout << "Queues are equal." << std::endl; else std::cout << "Queues are not equal." << std::endl; return 0; }
Output
Output of the above code is as follows −
Queues are equal.
Example
Consider the another scenario, where we are going to initialize the queues of different size and applying the operator==() function.
#include <iostream> #include <queue> int main() { std::queue<int> a, b; a.push(11); a.push(222); b.push(1); if (a == b) { std::cout << "Queues are equal."; } else { std::cout << "Queues are not equal."; } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Queues are not equal.
Example
In the following example, we are going to apply the operator==() function on the empty queue and observing the output.
#include <iostream> #include <queue> int main() { std::queue<int> a, b; if (a == b) { std::cout << "Queues are equal."; } else { std::cout << "Queues are not equal."; } return 0; }
Output
If we run the above code it will generate the following output −
Queues are equal.
Example
Following is the example, where we are going to initialize the queue with same number of elements with different values and applying operator==() function.
#include <iostream> #include <queue> int main() { std::queue<int> x, b; x.push(11); x.push(2); b.push(222); b.push(33); if (x == b) { std::cout << "Queues are equal."; } else { std::cout << "Queues are not equal."; } return 0; }
Output
Following is the output of the above code −
Queues are not equal.