Reputation: 3075
How can I check whether the first "n" elements of two vectors are equal or not?
I tried the following:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
typedef vector<double> v_t;
int main(){
v_t v1,v2;
int n = 9;
for (int i = 1; i<10; i++){
v1.push_back(i);
v2.push_back(i);
}
v1.push_back(11);
v2.push_back(12);
if (v1.begin()+n == v2.begin()+n)
cout << "success" << endl;
else
cout << "failure" << endl;
}
Why does it print "failure" and not "success"?
Upvotes: 54
Views: 85401
Reputation: 11
Be careful trying to use the operator==() for comparing Vectors. I read multiple answers on this topic on various forums, but nobody seems to mention this only works as in the example below, for native types:
std::vector<int> v1{ 1, 3, 5, 7 }; // create a vector with 4 elements
std::vector<int> v2{ 1, 3, 5, 7 }; // let's make this vector the same
// now compare:
if (v1 == v2) {
std::cout << "SUCCESS!!!" << std::endl;
}
Yes, the above code will work! (I just tried it. :)
However, the following code will not work without quite a bit of effort:
class Foo {
public:
int m_num = { 7 };
Foo(int n=0) : m_num(n) {}
};
// Create our comparison vectors filled with Objects of type Foo:
std::vector<Foo> foo1{ 1, 3, 5, 7 };
std::vector<Foo> foo2{ 1, 3, 5, 7 };
// This will not compile:
if (foo1 == foo2) {
std::cout << "SUCCESS!!!" << std::endl;
}
After some aggravation, I did find a relatively painless way to make the above comparison, however:
// First create a predicate function to perform the equivalency test:
bool match(Foo& f1, Foo& f2)
{
return f1.m_num == f2.m_num;
}
// Then use std::equal() like so:
if (std::equal(foo1.begin(), foo1.end(), foo2.begin(), foo2.end(),match))
{
std::cout << "### VECTORS MATCH!!! ###" << std::endl;
}
Hope this helps save someone the same aggravation I just went through trying to figure out how to make this work.
Upvotes: 0
Reputation: 163347
Use the std::equal
function from the <algorithm>
header:
if (std::equal(v1.begin(), v1.begin() + n, v2.begin()))
std::cout << "success" << std::endl;
Note that both vectors must have at least n
elements in them. If either one is too short, behavior of your program will be undefined.
If you want to check whether the entire vector is equal to the other, just compare them like you'd compare anything else:
if (v1 == v2)
Your (failed) code was comparing an iterator of one vector with an iterator of the other. Iterators of equal vectors are not equal. Each iterator is tied to the sequence it's iterating, so an iterator from one vector will never be equal to the iterator of another.
Upvotes: 152
Reputation: 383
First, there is no need to keep track the size of a vector, i.e. n
is useless; begin(v) + n == end(v)
or just n == size(v)
(the size information is in the vector class).
Now, I just want to point out a C++20 feature, the ranges library. It simplifies a lot of standard algorithms' function signatures, for instance the current "best" way to solve your problem would be:
std::ranges::equals(v1, v2); // returns a bool
Instead of the previous std::equals(begin(v1), end(v1), begin(v2))
. Also, if your ranges are more complex (i.e. vector of a class), you should consider the projection feature (if you want to apply a function before the comparaison, or compare a given member variable etc.).
Upvotes: 1
Reputation: 106226
The easiest (in terms of fewest non-everyday functions to look up) way to compare the two is to loop again:
bool are_equal = true;
for (int i = 0; i < first_how_many; i++)
if (v1[i] != v2[i])
{
are_equal = false;
break;
}
It'll do much the same thing, but if you prefer you can use the <algorithm>
header's std::equal
function: http://www.cplusplus.com/reference/algorithm/equal/
Upvotes: 7