一、遍历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
微信扫一扫
支付宝扫一扫