c++ - 自定义类 std::map 奇怪的行为
问题描述
在使用我创建为键的自定义类在地图中搜索键时,我遇到了一种奇怪的行为。
尽管它们存在于地图中,但它似乎找不到键。
有谁知道这是什么原因?
代码(可以在这里运行):
#include <iostream>
#include <map>
using namespace std;
typedef short int dimension;
class Point {
public:
Point() : m_x(0), m_y(0) {}
Point(const Point &other) : m_x(other.m_x), m_y(other.m_y) {};
Point(dimension x, dimension y) : m_x(x), m_y(y) {}
bool operator<(const Point &other) const {
if (m_x < other.m_x) return true;
return (m_y < other.m_y);
}
private:
dimension m_x;
dimension m_y;
};
int main() {
map<Point, bool> points = {{Point(0, 2), true},
{Point(1, 1), true},
{Point(2, 4), true}};
cout << (points.find(((points.begin())->first)) == points.end()) << endl;
cout << (points.find((next(points.begin()))->first) == points.end()) << endl;
cout << (points.find((next(next(points.begin())))->first) == points.end()) << endl;
cout << (points.find(Point(0, 2)) == points.end()) << endl;
cout << (points.find(Point(1, 1)) == points.end()) << endl;
cout << (points.find(Point(2, 4)) == points.end()) << endl;
return 0;
}
输出:
1
0
0
0
1
0
解决方案
你operator<
的有缺陷:
bool operator<(const Point &other) const {
if (m_x < other.m_x) return true;
return (m_y < other.m_y);
}
例如
{0, 3} < {1, 2} -> true
但
{1, 2} < {0,3} -> true
operator<
必须执行严格的弱排序。您忘记考虑(m_x > other.m_x)
应该导致的情况false
。
避免此类问题的一个巧妙技巧是使用std::tie
和利用std::tuple::operator<
:
bool operator<(const Point &other) const {
return std::tie(m_x,m_y) < std::tie(other.m_x,other.m_y);
}
推荐阅读
- c# - 如何修复 UNITYTLS_X509VERIFY_NOT_DONE
- c - make:系统找不到指定的文件(错误2)
- python - 无法停止 ThreadPoolExecutor
- mongodb - Docker 容器中的 MongoDB
- c# - C# 打卡 / 时钟小时 - 总工作时间
- r - 一次引入多个文件时,R仅输出最后一个文件的数据
- ios - HTML Drop Down 未在 iPhone 的 IOS 13 中选择选项
- php - 警告:mysqli_stmt_bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配
- javascript - 检测操作系统并重定向到安装的移动应用程序
- flutter - 如何在颤动中将 InputText 转换为 Text Widget