使用Q遍历QMap [英] Iterating over a QMap with for

查看:2653
本文介绍了使用Q遍历QMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QMap对象,我正在尝试将其内容写入文件.

I've a QMap object and I am trying to write its content to a file.

QMap<QString, QString> extensions;
//.. 

for(auto e : extensions)
{
  fout << e.first << "," << e.second << '\n';
}  

为什么会得到:error: 'class QString' has no member named 'first' nor 'second'

e不是QPair类型吗?

推荐答案

如果要使用firstsecond的STL样式,请执行以下操作:

If you want the STL style with first and second, do this:

for(auto e : extensions.toStdMap())
{
  fout << e.first << "," << e.second << '\n';
}

如果您想使用Qt提供的功能,请执行以下操作:

If you want to use what Qt offers, do this:

for(auto e : extensions.keys())
{
  fout << e << "," << extensions.value(e) << '\n';
}

这篇关于使用Q遍历QMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆