Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare vector of vectors

I have a vector of vectors that store pointers. Currently I iterate over them and compare each pointer and if I find ones that are not equal then vectors also do not equal, but I wonder if it is the right way to do such a thing.

UPD: std::vector<std::vector<Combination*> > combinations;

like image 627
Stals Avatar asked Oct 20 '25 03:10

Stals


1 Answers

If you're comparing the pointers themselves then you can just use operator== on the whole vector (of vectors). What it does is first checks if the sizes of the two vectors are equal. If they are, it subsequently compares each element using operator==. So this works recursively if you have a vector of vectors, or a vector of vectors of vectors. Though it's not technically recursion, since each operator== is actually a different function.

If you're dereferencing the pointers, the way you're doing it is fine. But for that case you could consider using std::equal with an appropriate predicate.

like image 151
Benjamin Lindley Avatar answered Oct 22 '25 18:10

Benjamin Lindley