Since your vector stores pointers, std::find
will compare the pointers, which will not invoke Location::operator==()
to compare the elements.
You need to use std::find_if
along with lambda:
return std::find_if(locations.begin(),
locations.end(),
[l](Location const *x) {
return *l == *x; //invoke operator=
}) != locations.end();
The lambda dereferences the pointers, and then uses ==
invoke the Location::operator=
.
If the location objects are not huge, I would advise you to use std::vector<Location>
instead of std::vector<Location*>
. If you use std::vector<Location>
, then you can use std::find
and the code will be simplied.
Even if location objects are huge, it is better to use smart pointer instead of raw pointer.
Hope that helps.