std::remove
comes in two flavors:
The std::remove
that you used takes a value as third parameter and compares it to the elements using the equality operator ==
. Your Node
class does not seem to support those comparisons, therefore you get the error somewhere deep in the guts of the standard library, where the comparison is made.
The alternative is std::remove_if
. Instead of the value to compare, it takes a callable as third parameter. It passes the objects in the range to that callable and expects a result convertible to bool, to tell if the element should be removed or not.
So, if you want your Node
s to be comparable with ==
in general, you will have to define that operator and can then use std::remove
:
bool operator== (Node const& lhs, Node const& rhs) {
return lhs.id == rhs.id && lhs.position == rhs.position;
}
And the just call remove
as you had it.
The other option is to provide a function object to std::remove_if
. That could be done either via a C++11 lambda expression, or by providing a handwritten functor in C++03:
C++11:
candidateNodes.erase (
std::remove_if (
candidateNodes.begin(),
candidateNodes.end(),
//here comes the C++11 lambda:
[&](Node const& node) {
return node.id == candidateNode.id && node.position == candidateNode.position;
}
),
candidateNodes.end()
);
Or C++03:
struct cmpNode {
Node const& candidateNode;
cmpNode(Node const& cn) : candidateNode(cn) {}
bool operator()(Node const& node) {
return node.id == candidateNode.id && node.position == candidateNode.position;
}
};
candidateNodes.erase(
std::remove_if(
candidateNodes.begin(),
candidateNodes.end(),
//here comes the C++03 functor:
cmpNode(candidateNode);
),
candidateNodes.end()
);