The C++ function std::stack::operator== function is a binary operator that tests whether two stacks are equal or not.
This function does not accept any parameter and is a const member function. The "const" keyword indicates that the function does not modify the stack. It returns a Boolean value true if the elements and size of the two stacks are equal, and false otherwise.
A binary operator takes two operands to perform a specific operation, such as addition, subtraction, multiplication, division, or comparison. It is denoted by a symbol or a keyword, such as +, -, *, /, and ==.Syntax
Following is the syntax for std::stack::operator== function −
bool stack1 == stack2Parameters
Returns true if both stacks are equal, otherwise false.
Example 1The following example shows the usage of the std::stack::operator== function by comparing two stacks containing the same elements and having the same size.
First, we are creating two stacks 's1' and 's2', and inserting the elements '1 - 5' into both stacks. Then we are comparing them using the operator== function.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; for (int i = 0; i < 5; ++i) { s1.push(i + 1); s2.push(i + 1); } if (s1 == s2){ cout << "Both stacks are equal." << endl; }else { cout << "Both stacks are not equal." << endl; } }Output
Let us compile and run the above program, this will produce the following result −
Both stacks are equal.Example 2
Here, we are comparing two non-empty stacks containing different elements but having the same size.
Initially, we are creating two stacks 's1' and 's2', and inserting the elements '1', '2', and '3' into 's1' and '3', '2', and '1' into 's2'. Then we are comparing them using the operator== function.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; s1.push(1); s1.push(2); s1.push(3); s2.push(3); s2.push(2); s2.push(1); if (s1 == s2) { cout << "Both stacks are equal." << endl; }else { cout << "Both stacks are not equal." << endl; } }Output
If we run the above code it will generate the following output −
Both stacks are not equal.Example 3
Now, we are comparing two non-empty stacks containing different elements and having different sizes.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; s1.push(1); s1.push(2); s2.push(1); if (s1 == s2) { cout << "Both stacks are equal." << endl; }else { cout << "Both stacks are not equal." << endl; } }Output
Following is an output of the above code −
Both stacks are not equal.Example 4
In the following example, we are trying to compare two empty stacks 's1' and 's2' using the operator== function −
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s1; stack<int> s2; if (s1 == s2) { cout << "Both stacks are equal." << endl; }else { cout << "Both stacks are not equal." << endl; } }Output
Output of the above code is as follows −
Both stacks are equal.
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