自定义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
发布评论