使用vector std::find和用户创建的对象(第三个参数)
Using vector std::find with a user-created object - third argument
我试图利用std::find
来搜索向量并返回找到所需对象的迭代器。我遇到的问题是,我不确定把什么作为第三个参数。下面是相关的代码行以及我正在使用的对象的定义。
功能:
vector<Vertex>::const_iterator findV = find(testV.begin(), testV.end(), vtx);
//vtx is of type Vertex
类定义:
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
类重载==
操作符,以便std::find
理解何时一个顶点与另一个顶点相同。这就是本杰明想带你去的地方。
std::find
的代码可以在http://www.cplusplus.com/reference/algorithm/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;
}
相关文章:
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Arduino C++在构造函数中用参数声明对象数组
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- C++ 类对象作为函数参数
- 如何将对象数组作为参数传递给模板
- 使用向量初始化参数化构造函数的对象数组
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 如何编写将展开以定义具有模板参数的对象的宏
- C++类对象 - 遍历基于参数的对象
- 将指针传递到成员的指针,从模板参数包到函数对象
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 是否可以获取成员函数模板参数的拥有对象?
- C++多参数对象创建
- 在不创建参数对象的情况下解析constexpr函数
- 推断模板参数对象成员的类型
- 如何解析c++ v8中的参数对象
- 当默认参数对象被销毁时
- 为什么函数不能更改其参数对象的地址/引用值?