[QT] QMap使用详解
引言
QMap/QMultiMap属于关联式容器,其底层结构是通过二叉树实现,故其查找value的效率很快。QMap中的数据都是成对出现的,第一个称为key(键),第二个称value(键值)。
目录
实例化QMap对象插入数据移除数据遍历数据由键查找对应键值由键值查找键修改键值查找是否包含某个键获取所有的键和键值清楚数据一个键对应多个值QMultiMap遍历数据
实例化QMap对象
/* 创建QMap实例, 第一个参数为QString类型的键,第二个参数为int类型的值 */
QMap map;
插入数据
/* 插入数据 两种方式*/
map["math"] = 100;
map.insert("English", 99); /* 推荐 */
打印输出: QMap((“English”, 99)(“math”, 100))
移除数据
/* 移除数据 */
map.remove("math");
打印输出:QMap((“English”, 99))
遍历数据
/* 遍历数据 (先随便插入几个)*/
map.insert("Math", 100);
map.insert("Chinese", 98);
map.insert("physical", 97);
map.insert("chemical", 96);
map.insert("biological", 95);
/* 遍历数据要使用迭代器,QT提供了两种方式的迭代 */
/* 第一种是Java类型的迭代 */
QMapIterator iterator(map);
while (iterator.hasNext()) {
iterator.next();
qDebug()
qDebug() |