迭代器查找自定义类vector的方法

iterator find method for custom class vector

本文关键字:方法 vector 查找 自定义 迭代器      更新时间:2023-10-16

我想使用迭代器的find方法来检查我是否已经在向量中定义了类的实例。我重载了类的==操作符。但是,我无法让它编译。

我在这里错过了什么?

谢谢你。

下面是代码片段:

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;
}

操作符应该接受对ContourEdgeIndexes的常量引用,如果它被定义为成员。操作符本身也应该是const。

bool operator == (const ContourEdgeIndexes& rhs) const {
    return ((this->first == rhs.first) && (this->second == rhs.second));
}
相关文章: