[英]Replace object in vector using iterator
这是我的对象类:
class person
{
public:
int id;
Rect rect;
};
总的来说,我正在遍历persons
向量,当找到匹配项时,我想将rect
更新为某个新的rect
甚至替换整个新的对象person
。
Rect mr = boundingRect(Mat(*itc));
person per;
vector <person> persons;
vector <person>::iterator i;
i = persons.begin();
while (i != persons.end()) {
if ((mr & i->rect).area() > 0) {
rectangle(frame, mr, CV_RGB(255, 0, 0));
putText(frame, std::to_string(i->id).c_str(), mr.br(),
FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 255));
replace(persons.begin(), persons.end(), i->rect, mr); // this line causes error
break;
} else {
...
}
我在用注释标记的行上遇到的错误是:
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'person' (or there is no acceptable conversion)
还有这个:
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)
我试图erase
该对象并添加一个新对象,但是我仍然遇到相同的错误。 我已经阅读了C ++从矢量中删除对象,但是不确定这是否是我的问题,并且我没有使用C ++ 11,所以这些解决方案对我不起作用。
迭代器和我的person
对象进行比较时是否有问题? 我认为这是解决办法,但不知道。
如果要比较person
类型的对象与Rect
类型的对象(这是您要replace
含义),则必须在Person
类中提供适当的比较运算符,例如:
bool operator== (const Rect &r) const { ... }
类似地,您需要一个具有签名(和可能的实现)的赋值运算符,如下所示:
person& operator= (const Rect &r) { rect = r; return *this; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.