
- 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 tests whether two stacks are not equal.
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 is the complement (opposite) operator of the operator== function. It returns a Boolean value true if the elements and size of the two stacks are not 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 != stack2
Parameters
- stack1 − First stack.
- stack2 − Second stack.
Return value
Returns true if both stacks are unequal, otherwise false.
Example 1
The following example shows the usage of 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 not equal." << endl; }else { cout << "Both stacks are 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 not equal." << endl; }else { cout << "Both stacks are 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 not equal." << endl; }else { cout << "Both stacks are 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 not equal." << endl; }else { cout << "Both stacks are equal." << endl; } }
Output
Output of the above code is as follows −
Both stacks are equal.