C++ <vector> - operator!= Function
The C++ <vector> operator!= function is used to check whether two vectors are unequal or not. It returns true if two vectors are not equal, else returns false. operator!= checks the size of both vectors, if sizes are same then it compares elements of vectors sequentially and stops comparison after first mismatch.
Syntax
template <class T, class Alloc> bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
template <class T, class Alloc> bool operator!= (const vector<T,Alloc>& lhs, const vector<T,Alloc>& rhs);
Parameters
lhs |
First vector. |
rhs |
Second vector. |
Return Value
Returns true if the contents of lhs are not equal to the contents of rhs, else returns false.
Time Complexity
Linear i.e, Θ(n).
Example:
In the example below, operator!= function is used to check whether two vectors are unequal or not.
#include <iostream> #include <vector> using namespace std; int main (){ vector<int> vec1 {10, 20, 30}; vector<int> vec2 {10, 20, 30}; vector<int> vec3 {10, 20}; if (vec1 != vec2) cout<<"vec1 and vec2 are not equal.\n"; else cout<<"vec1 and vec2 are equal.\n"; if (vec1 != vec3) cout<<"vec1 and vec3 are not equal.\n"; else cout<<"vec1 and vec3 are equal.\n"; return 0; }
The output of the above code will be:
vec1 and vec2 are equal. vec1 and vec3 are not equal.
❮ C++ <vector> Library