[英]How to remove one item from a vector of objects in c++?
我有以下C ++類,
class rec
{
public:
int width;
int height;
};
在我的主要功能中,我有一個帶有rec
對象的向量,
rec r1,r2,r3;
r1.height = r1.width = 1;
r2.height = r2.width = 2;
r3.height = r3.width = 3;
vector<rec> rvec = { r1,r2,r3 };
現在,我想使用以下方法調用從rvec
刪除一項:
rvec.erase(remove(rvec.begin(), rvec.end(), r_remove), rvec.end());
但是我得到了這個錯誤:
C2678:二進制'==':未找到采用'rec'類型的左操作數的運算符(或沒有可接受的轉換)
您需要為您的自定義數據結構rec
重載operator ==
class rec
{
public:
int width;
int height;
bool operator==(const rec& rhs) {
return (width == rhs.width) && (height == rhs.height);
}
};
因為remove
通過operator ==比較值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.