
- 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>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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