In this C++ tutorial, you shall learn how to check if two given vectors are equal using Equal-to operator, with example programs.

Check if Vectors are equal in C++

To check if two vectors are equal in C++, we can use C++ Equal-to Comparison Operator.

Equal-to operator can take the two vectors as operands, and returns true if the two vectors are equal or false if the two vectors are not equal.

The syntax of the boolean condition to check if vector v1 equals vector v2 is

v1 == v2

C++ Program

In the following program, we take two integer vectors in v1 and v2, and check if they are equal using Equal-to operator.

main.cpp

#include <iostream>
#include <vector>
using namespace std;

int main() {
   //int vectors
   vector<int> v1 { 2, 4, 6, 8, 10 };
   vector<int> v2 { 2, 4, 6, 8, 10 };

   //check if vectors are equal
   if (v1 == v2) {
      cout << "two vectors are equal.";
   } else {
      cout << "two vectors are not equal.";
   }
}

Output

two vectors are equal.
ADVERTISEMENT

Conclusion

In this C++ Tutorial, we learned how to check if two vectors are equal using Comparison Operator – Equal-to.