版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题:QMap使用     本文地址:https://www.techieliang.com/2017/12/537/

1. 简单范例

QMap与std::map相同,会自动根据key(第一项)进行升序排列

  1. QMap<QString,int> m_map;
  2. m_map["a"] = 10;//插入方式1
  3. m_map["as"] = 13;
  4. m_map.insert("b",22);//插入方式2
  5. m_map.insert("ba",23);
  6. auto find_index = m_map.find("as");//搜索
  7. if(find_index!=m_map.end()) {//返回为end表明未搜索到
  8. qDebug()<<find_index.key()<<find_index.value();
  9. }
  10. qDebug()<<m_map.value("a");//直接搜索,根据值key找值
  11. qDebug()<<m_map.value("aa");//没这项
  12. qDebug()<<m_map.key(13);//根据值找key
  13. qDebug()<<m_map.key(14);//没这项

返回结果:

  1. "as" 13
  2. 10
  3. 0
  4. "as"
  5. ""

相关帮助文档请见官网

erase删除某项,由于map的key具有唯一性,可以通过m_map[XX]=YY;修改已有项的值,若此项并不存在则会创建新的。

2. 其他

2.1. value/key方法返回值

value若查找不到目标会返回默认值,对于默认值得解释:

Returns a list containing all the values in the map, in
ascending order of their keys. If a key is associated with multiple
values, all of its values will be in the list, and not just the most
recently inserted one.

如果没有主动设置默认值,返回Qt默认值,此值在文档的容器介绍有说明

The documentation of certain container class functions refer to default-constructed values; for example, QVector automatically initializes its items with default-constructed values, and QMap::value()
returns a default-constructed value if the specified key isn’t in the
map. For most value types, this simply means that a value is created
using the default constructor (e.g. an empty string for QString). But for primitive types like int and double,
as well as for pointer types, the C++ language doesn’t specify any
initialization; in those cases, Qt’s containers automatically initialize
the value to 0.

key方法若找不到相应key,同样返回默认值。

2.2. QMap与std::map

QMap使用Iterator.key(),和Iterator.value()方法获取第一个或第二个元素的值。

而std::map使用Iterator->first(), Iterator->second()来获取第一个或第二个元素的值

2.3. std::map<Key, T> QMap::toStdMap() const

QMap实现了与std::map相互转换,toStdMap转到std,使用QMap的构造函数从std::map转到QMap

  1. QMap(std::initializer_list<std::pair<Key, T> > list)
  2. QMap(const QMap<Key, T> &other)
  3. QMap(QMap<Key, T> &&other)
  4. QMap(const std::map<Key, T> &other)//可以导入std::map
转载请以链接形式标明本文标题和地址:Techie亮博客 » QMap使用

QMap使用的更多相关文章

  1. 第37课 深度解析QMap与QHash

    1. QMap深度解析 (1)QMap是一个以升序键顺序存储键值对的数据结构 ①QMap原型为 class QMap<K, T>模板 ②QMap中的键值对根据Key进行了排序 ③QMap中 ...

  2. QMap

    #include <QCoreApplication> #include<QMap> #include<QDebug> int main(int argc, cha ...

  3. QMap与QHash

    关联容器可以保存任意多个具有相同类型的项,且它们由一个键索引.Qt提供两个主要的关联容器类:QMap<K, T>和QHash<K, T>. QMap<K, T>是一 ...

  4. QVariant类学习(非常强大的类型,甚至能处理QMap<QString ,QVariant>)

    详细描述: QVariant类作为一个最为普遍的Qt数据类型的联合. 因为c++禁止没有构造函数和析构函数的联合体,许多继承的Qt类不能够在联合体当中使用.(联合体当中的变量共用一个存储区),没有了联 ...

  5. c++ map与 qt QMap insert 区别

    当插入相同key的字段时, c++  map 会保留原来的字段, QMap 则会取代原来的字段.

  6. 1.QT中的容器QVector,QList,QSet,QMap,QQueue,QStack,QMultiMap,QSingleList等

    1  新建一个项目 在pro文件中只需要加上CONFIG += C++11 main.cpp #include <QMap> int main() { QMap<int,QStrin ...

  7. QMap迭代器

    QMap<int, QString> intToStr; intToStr[] = "test" for (auto iter = intToStr.begin(); ...

  8. QT 信号槽connect中解决自定义数据类型或数组作为函数参数的问题——QT qRegisterMetaType 注册MetaType——关键:注册自定义数据类型或QMap等容器类

    一般情况下信号槽直接连接方式不会出现问题,但是如果信号与槽在不同线程或Qt::QueuedConnection方式连接,可能会在连接期间报以下类似问题,如: QObject::connect: Can ...

  9. Qt532.容器QMap&QMultiMap

    PS: QMap 一个Key 只能对应 一个Value (不是绝对的情况...内部都有 一个key对应多个value的机制) PS: QMultiMap 一个Key 可以对应 多个Value PS:  ...

  10. 遍历QMap引发异常处理

    引言 用常规方法遍历QMap,删除满足条件元素时出现“读取位置0xXXX时发生访问冲突”.查看“调用堆栈”指向QMap<int,int>::iterator::operator++()和Q ...

随机推荐

  1. C-编译器的实现

    写这个编译器的目的,是为了完成编译原理课上老师布置的大作业,实际上该大作业并不是真的实现一个编译器,而我选择硬刚,是为了完成我的小愿望--手写内核,编译器和CPU.我花了整个上半学期,写完了WeiOS ...

  2. PG 存储函数调用变量的3种方法。

    一.假设有表student,字段分别有id,remark,name等字段. 二.写一个存储函数,根据传过去的变量ID更新remark的内容. 调用该存储函数格式如下:select  update_st ...

  3. 关于Win10 环境下Quartus II 15.0器件列表无法下拉的解决方法

    不知大家在Windows 10 64位系统环境下使用Quartus II 15.0在新建工程时遇到过这种问题没,在新建工程的过程是选择器件的列表无法下拉,只能看到一个器件型号,如图1所示. 图1 开始 ...

  4. Verilog的一些系统任务(二)

    $monitor 任务$monitor提供了监控和输出参数列表中的表达式或变量值的功能. 格式: $monitor(p1,p2,...,pn);   $monitor;        $monitor ...

  5. 实验一 Java开发环境的熟悉(Linux+Eclipse)

    实验一 Java开发环境的熟悉(Linux+Eclipse) 实验内容及步骤 使用JDK编译.运行简单的Java程序 打开windows下的cmd → 输入cd Code命令进入Code目录 → 输入 ...

  6. sql语句-6-更新数据

  7. python 多线程笔记(4)-- 车站售票模拟

    import threading import time import random class Worker(threading.Thread): '''售票员''' def __init__(se ...

  8. day2 RHCE

    1.配置SELINUX 在system1和system2上要求SeLinux的状态为enforcing.要求系统重启后依然生效. server [root@server0 ~]# getenforce ...

  9. 4709: [Jsoi2011]柠檬

    4709: [Jsoi2011]柠檬 https://www.lydsy.com/JudgeOnline/problem.php?id=4709 分析: 决策单调性+栈+二分. 首先挖掘性质:每个段选 ...

  10. 实时备份工具之inotify+rsync

    1.inotify简介 inotify 是一个从 2.6.13 内核开始,对 Linux 文件系统进行高效率.细粒度.异步地监控机制, 用于通知用户空间程序的文件系统变化.可利用它对用户空间进行安全. ...