Is there any way to compare two vectors?
if (vector1 == vector2)
DoSomething();
Note: Currently, these vectors are not sorted and contain integer values.
Check std::mismatch
method of C++.
comparing vectors has been discussed on DaniWeb forum and also answered.
Check the below SO post. will helpful for you. they have achieved the same with different-2 method.
Your code (vector1 == vector2
) is correct C++ syntax. There is an ==
operator for vectors.
If you want to compare short vector with a portion of a longer vector, you can use theequal()
operator for vectors. (documentation here)
Here's an example:
using namespace std;
if( equal(vector1.begin(), vector1.end(), vector2.begin()) )
DoSomething();
According to the discussion here you can directly compare two vectors using
==
if (vector1 == vector2){
//true
}
else{
//false
}
If they really absolutely have to remain unsorted (which they really don't.. and if you're dealing with hundreds of thousands of elements then I have to ask why you would be comparing vectors like this), you can hack together a compare method which works with unsorted arrays.
The only way I though of to do that was to create a temporary vector3
and pretend to do a set_intersection
by adding all elements of vector1
to it, then doing a search for each individual element of vector2
in vector3
and removing it if found. I know that sounds terrible, but that's why I'm not writing any C++ standard libraries anytime soon.
Really, though, just sort them first.
来源:https://stackoverflow.com/questions/6248044/c-comparing-two-vectors