I have 2 vectors, with the type of a pointer to Point to take advantage of polymorphism idea, because I have some type of points classes that inherit from the Point class.
vector<Point*> previous_points;
vector<Point*> current_points;
init_points(previous_points);
init_points(current_points);
do_some_proccess(previous_points);
do_some_proccess(current_points);
cout << (previous_points == current_points) << endl; // returns 0
the == operator returns me 0 even though the contents are the same, and the reason is (from what i read and understood) that it is comparing the vector contents which are pointers and the pointers are not the same of course (init_points creates new objects with same content for each of the vectors).
my question is, can i still use the comparison operator and get it compare the content of the pointers in my vectors and not the pointer themselves? or do i have to implement my own is_content_equals function to do that?
edit: I have implemented the equals == operator inside the Point class so it is comparing the content of point.
thanks
You don't have to roll out your own function. You can use std::equal
, with a suitable predicate. This assumes a working operator==
for Point
.
bool ok = equal(begin(previous_points), end(previous_points),
begin(current_points), end(current_points),
[](const Point* lhs, const Point* rhs){ return *lhs == * rhs; });
Pre C++14, you can use this overload. Here, it is up to you to ensure that current_points
is the same length as previous_points
.
bool ok = equal(begin(previous_points), end(previous_points),
begin(current_points),
[](const Point* lhs, const Point* rhs){ return *lhs == * rhs; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With