将向量 std::find 与用户创建的对象一起使用 - 第三个参数

Using vector std::find with a user-created object - third argument

我正在尝试使用 std::find 来搜索向量,并且 return 找到了所需对象的迭代器。我遇到的问题是我不确定将什么作为第三个参数。下面是相关的代码行以及我正在使用的对象的定义。

函数:

vector<Vertex>::const_iterator findV = find(testV.begin(), testV.end(), vtx); 
//vtx is of type Vertex

Class定义:

class Vertex
{
    private:
        int currentIndex;
        double xPoint, yPoint, zPoint;
        vector<double> vertexAttributes;

    public:
        Vertex();
        ~Vertex();

        friend istream& operator>>(istream&, Vertex &);
        friend ostream& operator<<(ostream&, const Vertex &);

        double getIndex(){return currentIndex;}
        double get_xPoint(){return xPoint;}
        double get_yPoint(){return yPoint;}
        double get_zPoint(){return zPoint;}
};

逻辑上我会假设,因为我正在搜索类型 Vertex 的对象,所以第三个参数也应该是这种类型,但这不起作用。

收到的错误是:

error: no match for 'operator==' (operand types are 'Vertex' and 'const Vertex')|

如果需要进一步说明,请告诉我。

谢谢

您需要为 Vertex class 重载 == 运算符,以便 std::find 理解何时一个顶点与另一个顶点相同。这就是本杰明试图带你去的地方。

可在 http://www.cplusplus.com/reference/algorithm/find/

找到 std::find 的代码
template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

希望该代码能够更清楚地说明正在发生的事情。