The C++ vector::operator!=() function is used to test whether two vectors are unequal or not, it returns true if the given two vectors are not equal or else it returns false. Operator !=() function first checks the size of both container, if sizes are same then it compares elements sequentially and comparison stops at first mismatch. This member function never throw an exception and the time complexity of the operator!=() function is linear.
SyntaxFollowing is the syntax for C++ vector::operator!=() Function −
template <class T, class Alloc> bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);Parameters
Let's consider the following example, where we are going to use the opertor!=() function.
#include <iostream> #include <vector> using namespace std; int main(void) { vector<int> v1; vector<int> v2; v1.resize(10, 100); if (v1 != v2) cout << "v1 and v2 are not equal" << endl; v1 = v2; if (!(v1 != v2)) cout << "v1 and v2 are equal" << endl; return 0; }Output
When we compile and run the above program, this will produce the following result −
v1 and v2 are not equal v1 and v2 are equalExample 2
Considering the another scenario, where we are going to take string values and aplying operator!=() function.
#include <iostream> #include <vector> using namespace std; int main (){ vector<string> myvector1 {"RS7","AUDI","MAYBACH GLS"}; vector<string> myvector2 {"RS7","AUDI","MAYBACH GLS"}; if (myvector1 != myvector2) cout<<"The Two Vectors are not equal.\n"; else cout<<"The Two Vectors are equal.\n"; return 0; }Output
On running the above program, it will produce the following result −
The Two Vectors are equal.Example 3
In the following example, we are going to push_back() function to insert the values and checked whether the vectors are equal or not.
#include <vector> #include <iostream> int main( ){ using namespace std; vector <int> myvector1, myvector2; myvector1.push_back( 11 ); myvector1.push_back( 113 ); myvector2.push_back( 222 ); myvector2.push_back( 113 ); if ( myvector1 != myvector2 ) cout << "Vectors are not equal." << endl; else cout << "Vectors are equal." << endl; }Output
When we execute the above program, it will produce the following result −
Vectors are not 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