The stack is a fundamental data structure that follows the LIFO(Last-In-First-Out) manner, meaning that the last added element will be removed first.
The C++ function std::stack::operator<= compares the two stacks, determining if the first stack is less than or equal to the second stack. It evaluates the stacks size and elements wise comparisons to determine the relationship.
SyntaxFollowing is the Syntax for std::stack::operator<= function −
bool operator<=(const stack <Type, Container>& left, const stack <Type, Container>& right);Parameters
It return true if the stack on the leftside is lessthan or equal to stack on the right side, otherwise false.
Example 1Let's look at the following where we are going to compare the elements inside the stack using the <= operator.
#include <iostream> #include <stack> int main(){ std::stack<int> x; x.push(1); x.push(2); x.push(3); if (x.top() <= 5) { std::cout << "Elements are Lessthan Or Equal to 5"; } else { std::cout << "Elements are Greaterthan 5"; } return 0; }Output
Let us compile and run the above program, this will produce the following result −
Elements are Lessthan Or Equal to 5Example 2
Considering the following example, where we are going to compare the size of the two stacks.
#include <iostream> #include <stack> int main(){ std::stack<int> x, y; x.push(11); x.push(25); y.push(16); if (x.size() <= y.size()) { std::cout << "X is Lessthan Or Equal To Y"; } else { std::cout << "X Has More Elements Compared To Y"; } return 0; }Output
If we run the above code it will generate the following output −
X Has More Elements Compared To YExample 3
In the following example, we are going to check if one stack is empty or lessthan or equal to another stack.
#include <iostream> #include <stack> int main(){ std::stack<int> a, b; a.push(12); if (a.empty() || a <= b) { std::cout << "True"; } else { std::cout << "False"; } return 0; }Output
Output of the above code is as follows −
False
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