Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ' == ' to compare two vectors. I tried it and seems to be working fine. But I don't know whether it will work in more complex situations

First example:

int main(){     using namespace std;        vector<int> v1{10, 20, 30, 40, 50};     vector<int> v2{10, 20, 30, 40, 50};      if(v1==v2)         cout<<"equal";     else         cout<<"unequal"; }   // it returns equal  

Second example:

int main(){     using namespace std;        vector<int> v1{10, 20, 30, 40, 50};     vector<int> v2{10, 20, 100000, 40, 50};      if(v1==v2)         cout<<"equal";     else         cout<<"unequal"; }   // it returns notequal  
like image 652
suman shadow Avatar asked May 07 '13 15:05

suman shadow


People also ask

How do you compare two vectors?

A vector quantity has two characteristics, a magnitude and a direction. When comparing two vector quantities of the same type, you have to compare both the magnitude and the direction. On this slide we show three examples in which two vectors are being compared. Vectors are usually denoted on figures by an arrow.

How do you know if two vectors are equal?

In simple words, we can say that two or more vectors are said to be equal vectors if their length is the same and they all point in the same direction. Generally, we can check for equal vectors by comparing their coordinates. If all the coordinates of two or more vectors are the same, then they are equal vectors.

What does the equality operator do with two vectors?

Description. The C++ function std::vector::operator== tests whether two vectors are equal or not. Operator == first checks the size of both container, if sizes are same then it compares elements sequentially and comparison stops at first mismatch.

Can you compare vectors of different dimensions?

If the vectors are homogeneous, for example, images of different dimensions, then another approach is possible to compare them. It is necessary to compare a smaller vector with all fragments of a larger vector with similar lengths. In this case, a part of a larger vector may exactly coincide with a smaller vector.


2 Answers

The overload of operator == that works on two std::vectors will compare the vector sizes and return false if those are different; if not, it will compare the contents of the vector element-by-element.

If operator == is defined for the vector's element type, then the comparison of vectors through operator == is valid and meaningful.

In formal terms, the C++11 standard specifies the operational semantics of a == b for sequence containers as (Table 96, § 23.2.1):

== is an equivalence relation.

distance(a.begin(), a.end()) == distance(b.begin(), b.end()) && equal(a.begin(), a.end(), b.begin())

As you can see, equality between sequence containers is defined in terms of the std::equal algorithm between ranges defined by pairs of iterators, which in turn uses operator == for comparison of individual elements.

like image 142
Andy Prowl Avatar answered Oct 19 '22 19:10

Andy Prowl


Yes, you can use operator== to compare two std::vectors. It will return true only if the vectors are the same size and all elements compare equal.

like image 45
Joseph Mansfield Avatar answered Oct 19 '22 18:10

Joseph Mansfield