c++ - 如何将 std::find 用于类向量?
问题描述
class g {
public:
int x, y, z;
};
vector<g>f;
int main() {
g obj1 = { 1,2,3 };
f.push_back(obj1);
auto it = find(f.begin(), f.end(), 2);
f.erase(it);
}
这段代码给了我一个 C2678 错误:二进制 '==': no operator found 它采用 'g' 类型的左操作数。
解决方案
您应该==
为您的类实现运算符:
class g {
public:
int x, y, z;
bool operator==(const g& o) const {
return x == o.x && y == o.y && z == o.z;
}
};
使用正确的类型作为参数:
auto it = std::find(f.begin(), f.end(), g{1, 2, 3});
推荐阅读
- python - 如何比较两个不同csv(旧的和新的)中的两列并在旧csv中存在值时更新第三列与熊猫
- amazon-s3 - 为什么 LogDNA 仅将部分数据积压到 Amazon S3?
- amazon-web-services - Terraform ECS / Github Actions 似乎忽略了 ALB 健康检查
- android - 当我们将 Jetpack Navigation 与 FragmentContainerView 一起使用时,如何避免片段重新创建?
- css - 使用 CSS 样式表更改闪亮的“确定”按钮 (confirmButtonCol) 的颜色
- android - 如何让手机模拟器运行手机应用程序而不佩戴操作系统应用程序?
- python - 为什么我的 ChoiceField 没有从我的 Django 表单呈现到模板上?
- javascript - 如何在需要时替换 debounceTime(0) 发射?
- sql - 通过 Azure 流分析作业使用时间戳时未写入数据
- firebase - 未处理的异常:'package:flutter/src/widgets/navigator.dart':断言失败:'!navigator._debugLocked':不正确