where is the problem vector find c++ ??
when i compile i has an error and i cannot understand where is the problem?
class Edge{
public:
int nid;
bool operator==(const Edge& 开发者_运维问答edge) const {
return nid == edge.nid;
}
};
and problem here
vector<Edge> edges;
vector<Edge>::iterator it;
it = find (edges.begin(), edges.end(), nid);
if( it != edges.end() )
edges.erase(it);
any ideas ?!!!?
find
will compare (with ==
) the objects of type Edge
in the vector with nid
. I guess that nid
is of type int
and that won't work unless you implement operator==
between Edge
and int
.
You can try :
it = find (edges.begin(), edges.end(), Edge(nid));
You haven't described the extract symptoms of your problem, but I guess that it is related to that didn't define the operator!=
You probably want to define it as
bool operator!=(const Edge&edge)const {
return !(*this == edge);
}
also if your find(...,nid) is taking nid as an integer argument, you probably also need to overload the == operator as
bool operator==(const int&edge)const {
return this->nid == edge;
}
You can either overload the operator!=, too, or simply negate the if condition.
Assuming nid is the same type as in the class I assume you get a complaint about the type of the third argument to find(). I think you can switch it to:
vector<Edge>::iterator it = find (edges.begin(), edges.end(), edges.begin() + nid);
精彩评论