遍历QMap

一、遍历QMap容器

QMap的特点是它是一种有序的键值对容器。我们可以按照键的顺序遍历所有的键值对,使用QMap的foreach循环即可。

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

foreach(QString key, map.keys())
{
    qDebug() << key << ":" << map.value(key);
}

输出结果为:

one : 1
three : 3
two : 2

可以看到,QMap会按照键的顺序进行遍历,输出的结果是有序的。

二、遍历QMap的key

有时候我们只需要遍历QMap的键,不需要访问每个键对应的值,这时候可以使用QMap::keys()函数获取所有键的列表,然后使用foreach循环遍历。

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

foreach(QString key, map.keys())
{
    qDebug() << key;
}

输出结果为:

one
three
two

三、遍历QMap 修改value

有时候我们需要在遍历QMap的过程中修改它的值。这时候不能直接修改值,因为QMap使用的是内部节点结构,直接修改值可能导致节点结构不一致,影响QMap的使用。

一种可行的方法是使用QMap::iterator。

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

for(QMap<QString, int>::iterator it = map.begin(); it != map.end(); ++it)
{
    QString key = it.key();
    int value = it.value();

    if(key == "two")
    {
        it.value() = 20;
    }
    qDebug() << key << ":" << value;
}

输出结果为:

one : 1
three : 3
two : 20

可以看到,我们使用QMap::iterator遍历QMap,然后使用迭代器的value()函数修改值。

四、遍历Map的几种方式

在遍历QMap的过程中,也可以使用STL中的算法库来进行遍历。

例如,使用std::for_each和Lambda表达式:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

std::for_each(map.begin(), map.end(), [](QMap<QString, int>::value_type pair){
    qDebug() << pair.first << ":" << pair.second;
});

输出结果与上面的例子一样。

还可以使用std::transform和std::ostream_iterator来将QMap输出到流中:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

std::transform(map.constBegin(), map.constEnd(), std::ostream_iterator<std::pair<QString, int>>(std::cout, ", "), [](QMap<QString, int>::value_type pair){
    return pair;
});

输出结果为:

("one", 1), ("three", 3), ("two", 2),

五、遍历QMap中所有的key

有时候我们需要查找QMap中的某一个key是否存在,可以使用QMap::contains()函数来检查。也可以遍历QMap的所有键,然后查找。

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

QString keyToFind = "two";
bool foundKey = false;
foreach(QString key, map.keys())
{
    if(key == keyToFind)
    {
        foundKey = true;
        break;
    }
}

if(foundKey)
{
    qDebug() << keyToFind << "found";
}
else
{
    qDebug() << keyToFind << "not found";
}

输出结果为:

two found

六、遍历QMap中所有的key qt

QMap类提供了一个keys()函数,可以返回一个QList类型的所有key列表。下面的例子演示了如何使用QList的一些基本函数,在遍历QMap中所有的key时,更好地操作数据。

QMap<QString, QString> map;
map.insert("one", "1");
map.insert("two", "2");
map.insert("three", "3");

foreach(QString key, map.keys())
{
    QString value = map.value(key);
    qDebug() << key << ":" << value;
}

QList<QString> keys = map.keys();

QString keyToFind = "two";
if(keys.contains(keyToFind))
{
    qDebug() << keyToFind << "found";
}
else
{
    qDebug() << keyToFind << "not found";
}

keys.removeOne(keyToFind);

foreach(QString key, keys)
{
    QString value = map.value(key);
    qDebug() << key << ":" << value;
}

keys.clear();

foreach(QString key, keys)
{
    qDebug() << key;
}

输出结果为:

one : 1
three : 3
two : 2
two found
one : 1
three : 3

七、遍历QMap中所有的key提示second不是成员

使用QMap::iteretor虽然方便,但是会提示“’second’ is not a member of QMap::iterator”错误,这是因为QMap::iterator是QMap::value_type类型的迭代器,而value_type是一个键值对类型。

解决这个问题的方法是在使用迭代器的时候,使用QMap::iterator::value()函数来获取值。

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

for(QMap<QString, int>::iterator it = map.begin(); it != map.end(); ++it)
{
    QString key = it.key();
    int value = it.value();

    if(key == "two")
    {
        it.value() = 20;
    }
    qDebug() << key << ":" << value;
}

输出结果与前面的例子一样。

八、QMap取值

获取QMap中某一个key对应的value,有两种方法。

一种是使用QMap::value()函数:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

int value = map.value("two", -1);
qDebug() << value;

输出结果为:

2

还可以使用QMap::operator[]运算符:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

int value = map["two"];
qDebug() << value;

输出结果为:

2

九、QMap迭代器遍历

QMap提供了两个类型的迭代器:QMap::const_iterator和QMap::iterator,常量迭代器用于遍历QMap时只能读取值,而非常量迭代器用于修改QMap中的值。

使用QMap::const_iterator类型的迭代器:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

for(QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
{
    QString key = it.key();
    int value = it.value();
    qDebug() << key << ":" << value;
}

输出结果与前面的例子一样。

使用QMap::iterator类型的迭代器:

QMap<QString, int> map;
map.insert("one", 1);
map.insert("two", 2);
map.insert("three", 3);

for(QMap<QString, int>::iterator it = map.begin(); it != map.end(); ++it)
{
    QString key = it.key();
    int value = it.value();

    if(key == "two")
    {
        it.value() = 20;
    }
    qDebug() << key << ":" << value;
}

输出结果与前面的例子一样。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/285046.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-22 15:43
下一篇 2024-12-22 15:43

相关推荐

  • Python遍历集合中的元素

    本文将从多个方面详细阐述Python遍历集合中的元素方法。 一、for循环遍历集合 Python中,使用for循环可以遍历集合中的每个元素,代码如下: my_set = {1, 2…

    编程 2025-04-29
  • Python如何遍历字典中的key和value

    本文将详细讲解Python中如何遍历字典中的key和value,包括多种遍历方式以及在遍历过程中的一些应用场景。 一、遍历字典中的key和value 在Python中,字典是一种无…

    编程 2025-04-29
  • 使用PHP foreach遍历有相同属性的值

    本篇文章将介绍如何使用PHP foreach遍历具有相同属性的值,并给出相应的代码示例。 一、基础概念 在讲解如何使用PHP foreach遍历有相同属性的值之前,我们需要先了解几…

    编程 2025-04-28
  • 二叉树非递归先序遍历c语言

    本文将为您详细介绍二叉树的非递归先序遍历算法,同时提供完整的C语言代码示例。通过本文,您将了解到二叉树的先序遍历算法,以及非递归实现的方式。 一、二叉树的先序遍历算法介绍 在介绍二…

    编程 2025-04-28
  • Python如何遍历列表

    在Python编程中,列表是一种常用的数据类型,它允许我们存储多个值。但是,我们如何遍历列表并对其中的每个值进行操作呢? 一、for循环遍历列表 fruits = [‘apple’…

    编程 2025-04-28
  • Python遍历字典删除元素

    本文主要介绍Python中如何遍历字典并删除元素。在实际应用中,遍历字典并删除元素是一种非常常见的操作,但需要注意的是,直接在字典中删除元素可能会改变字典中其他元素的索引顺序,因此…

    编程 2025-04-28
  • Python遍历文件夹中的shp文件

    对于GIS分析领域的开发工程师,遍历文件夹中的shp文件是一个常见的需求。Python提供了一种非常便捷的方法来实现这个功能。本文将从以下几个方面进行讲解: 一、`os`模块的使用…

    编程 2025-04-27
  • Python中遍历字符串中的数字两位数及其应用

    本文将从多个方面详细阐述Python中遍历字符串中的数字两位数的应用及实现方法。 一、提取字符串中的数字两位数 Python中提取字符串中的数字两位数可以使用正则表达式,具体代码如…

    编程 2025-04-27
  • Python中for循环遍历列表

    本文将全方位详细介绍Python中for循环遍历列表的方法和技巧,帮助您更加深入理解并灵活运用Python中的for循环。 一、for循环遍历列表的基础用法 在Python中使用f…

    编程 2025-04-27
  • forof遍历对象的详细阐述

    forof是一种ES6的语法糖,用于遍历可迭代对象。相较于传统的for循环和forEach方法,forof更加简洁、易读,并且可以遍历各种类型的数据。 一、基本语法 forof的基…

    编程 2025-04-25

发表回复

登录后才能评论