QMap之自定义Key和Value

编程入门 行业动态 更新时间:2024-10-15 20:18:18

QMap之<a href=https://www.elefans.com/category/jswz/34/1771438.html style=自定义Key和Value"/>

QMap之自定义Key和Value

为什么要自定义QMap的 key和value呢?因为在某些应用场景,基本数据类型的key和value并不能满足要求,比如座位在第几行、第几列,座位上人的姓名、职务;二维坐标系中散点的横坐标、纵坐标,散点的id、result。即key和value是一种多对多的关系。

#include <QMap>
#include <QDebug>#include <tuple>
#include <string>struct CustomKey
{
    CustomKey(int x, int y)
    {
        this->x = x;
        this->y = y;
    }    bool operator < (const CustomKey &key) const
    {
        // 先比较x与key.x
        // 然后比较y与key.y
        return std::tie(this->x, this->y) < std::tie(key.x, key.y);
    }    int x = 0;
    int y = 0;
};struct CustomValue
{
    std::string name;
    std::string duties;
};int main(int argc, char *argv[])
{
    QMap<CustomKey, CustomValue> positionMap;
    CustomValue user;
    user.name = "csdn";
    user.duties = "website";
    positionMap[CustomKey(0, 0)] = user;
    user.name = "cao shang pa&#

更多推荐

QMap之自定义Key和Value

本文发布于:2023-12-04 01:53:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1659418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   QMap   Key

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!