简体   繁体   中英

iterator find method for custom class vector

I want to use find method of iterator to check if an instance of class I have defined already in the vector or not. I have overloaded the == operator for the class. However, I couldn't get it to compile.

What am I missing here?

Thanks n advance.

here is a snippet from the code:

vector<ContourEdgeIndexes>::iterator it = find(contourEdges.begin(),contourEdges.end(),contourEdgeCand);
        if(it != contourEdges.end()) {
            contourEdges.erase(it);
        }

compiler gives this error:
error: no matching function for call to     ‘find(std::vector<ContourEdgeIndexes>::iterator, std::vector<ContourEdgeIndexes>::iterator, ContourEdgeIndexes&)’

edit:
and here is the overloaded == operator:
bool operator == (ContourEdgeIndexes& rhs) {
    if((this->first == rhs.first) && (this->second == rhs.second))
        return true;
    else
        return false;
}

Your operator should accept constant reference to ContourEdgeIndexes , if it defined as member. Also operator itself should be const.

bool operator == (const ContourEdgeIndexes& rhs) const {
    return ((this->first == rhs.first) && (this->second == rhs.second));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM