Quickly check if two STL vectors contain same elements or not
Unlike normal C/C++ arrays, we don’t need to do element by element comparison to find if two given vectors contain same elements or not.
In case of vectors, the operator “==” is overloaded to find the result quickly. Below is an example to demonstrate same.
C++14
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v1{3, 1, 2, 3};
vector< int > v2{3, 1, 2, 3};
(v1 == v2)? cout << "Equal\n" : cout << "Not Equal\n" ;
vector< int > v3{1, 2, 3, 4};
(v1 == v3)? cout << "Equal\n" : cout << "Not Equal\n" ;
vector< int > v4;
(v1 == v4)? cout << "Equal\n" : cout << "Not Equal\n" ;
vector< int > v5;
(v5 == v4)? cout << "Equal\n" : cout << "Not Equal\n" ;
return 0;
}
|
Output:
Equal
Not Equal
Not Equal
Equal
Last Updated :
16 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...