使用迭代器替换矢量中的对象
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; }
简化的现场演示
相关文章:
- 使用迭代器时如何访问对象在向量中的位置?
- std::list 中的迭代器感知对象
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 指向临时对象的自定义迭代器(延迟加载)
- 迭代器和标量对象之间的未定义行为有什么区别吗?
- 如何确定迭代器在 c++ 模板函数中指向的对象的类型?
- 在多个 STL 对对象上创建迭代器
- 访问执行策略for_each函数对象中的迭代器
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 无法使用迭代器在类对象的向量中读取向量
- C++ 向量::使用类对象迭代器擦除不擦除向量成员
- 对列表迭代器指向的对象不起作用的引用
- 设计具有两个指向嵌套对象的指针的迭代器
- 有关使用矢量迭代器访问对象方法的问题
- 使用迭代器指针 (C++) 从对象指针列表中删除元素
- 使用迭代器替换矢量中的对象
- 列表<对象*>迭代器中的分段错误