C++ Stack::operator< Function
The C++ function std::stack::operator< is a binary operator that checks whether the first stack is less than the other or not.
This function does not accept any parameter and returns a Boolean value true if the first stack is less than the other, and false otherwise.
When defining the operator< function for a class, it is important to ensure that it provides a strict weak ordering over the objects in the class. This means that the function must satisfy three properties: irreflexive (an object is not less than itself), asymmetry (if A < B then !(B < A)), and transitivity (if A < B and B < C, then A < C).
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< −
bool stack1 < stack2
Parameters
- stack1 − First stack.
- stack2 − Second stack.
Return value
Returns true if first stack is less than the second stack, otherwise false.
Example 1
The following example shows the usage of std::stack::operator< function by comparing two stacks 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 << "Stack s1 is less than s2." << endl;}
else {
cout << "Stack s1 is not less than s2." << endl;
}
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
Stack s1 is not less than s2.
Example 2
Here, we are creating two stacks 's1' and 's2', and then comparing them based on their top elements using the operator< function.
#include <iostream>
#include <stack>
using namespace std;
int main(void) {
stack<int> s1;
s1.push(1);
s1.push(2);
s1.push(3);
stack<int> s2;
s2.push(1);
s2.push(5);
if (s1.top() < s2.top()) {
cout << "s1 is smaller than s2\n";
} else {
cout << "s1 is not smaller than s2\n";
}
}
Output
If we run the above code it will generate the following output −
s1 is smaller than s2
Example 3
In the following example, we are defining a function 'getMinElement' that takes a reference to a stack of integers as input and returns the minimum element in the stack using another stack. Then we are creating a stack 's' of integers in the main function and pushing three values into it, and calling the 'getMinElement' function to get the minimum element in 's'.
#include <iostream>
#include <stack>
using namespace std;
int getMinElement(stack<int>& s) {
stack<int> minStack;
while (!s.empty()) {
int current = s.top();
s.pop();
if (minStack.empty() || current < minStack.top()) {
minStack.push(current);
} else {
minStack.push(minStack.top());
}
}
int minElement = minStack.top();
while (!minStack.empty()) {
minStack.pop();
}
return minElement;
}
int main() {
stack<int> s;
// populate s
s.push(48);
s.push(2);
s.push(103);
// get minimum element in s using another stack
int minElement = getMinElement(s);
cout << "Minimum element in s is " << minElement << endl;
return 0;
}
Output
Following is an output of the above code
Minimum element in s is 2