Skip to content
Related Articles

Related Articles

Quickly check if two STL vectors contain same elements or not
  • Difficulty Level : Basic
  • Last Updated : 23 Jun, 2017

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++ implementation to check whether elements
// in vector is equal or not
using namespace std;
// Check if all elements is equal or not
int main()
  // Comparing equal vectors
  vector<int> v1{3, 1, 2, 3};
  vector<int> v2{3, 1, 2, 3};
  (v1 == v2)?  cout << "Equal\n" : cout << "Not Equal\n";
  // Comparing non-equal vectors
  vector<int> v3{1, 2, 3, 4};
  (v1 == v3)?  cout << "Equal\n" : cout << "Not Equal\n";
  // comparing with empty
  vector<int> v4;
  (v1 == v4)?  cout << "Equal\n" : cout << "Not Equal\n";
  // comparing two empty
  vector<int> v5;
  (v5 == v4)?  cout << "Equal\n" : cout << "Not Equal\n";
   return 0;

Output :

Not Equal
Not Equal

This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :