c++ - 在 C++ 中将 QPoint 与 std::map 一起使用

标签 c++ qt dictionary point

我在 vector 中有 QPoint 变量,我想将其存储在 map 中。

std::map<QPoint, int> pointMap;
QPoint t;
int i;
pointMap.insert(std::pair<QPoint,int>(t, i));

如果我使用字符串,整数映射,它工作正常。但是我无法在 map 中使用 QPoint。有什么想法吗?

编译器消息:“与 (std::pair)(QPoint&, int) 的调用不匹配”

最佳答案

我认为问题在于std::map需要具有 operator < 的类型然而已实现QPoint才不是。要解决此问题,您可以通过以下方式定义 map :

std::map<int, QPoint> pointMap;

另外,您需要为 QPoint 定义自定义“小于”运算符,例如:

bool operator <(QPoint point1, QPoint point2)
{
    // Do you logic here, to compare two points.
    return true;
}

关于c++ - 在 C++ 中将 QPoint 与 std::map 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23338939/

相关文章:

c++ - 我的程序意外结束

c++ - 在结构中包含一个 std::map ?可以吗?

c++ - 如果迭代器未因插入而失效,则使用 std::find 和 C::insert() 线程安全

c++ - 在 C++ 中,如何访问继承类中的私有(private)基成员变量?

c++ - 如何使字符串相等?

c++ - Qt XML 输入

c++ - NetBeans 在外部终端运行

c++ - 在不扩展类的情况下为 QDialog 中的按键事件添加回调

c - 在 C 中制作一个 makefile

c# - 在.Net中创建只需要一个键即可访问的多键字典的方法是什么?