Iterating over a QMap with for

前端 未结 9 1711
执笔经年
执笔经年 2021-01-31 07:02

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

QMap extensions;
//.. 

for(auto e : extensions)
{
  fou         


        
9条回答
  •  春和景丽
    2021-01-31 07:47

    Another convenient method, from the QMap Docs. It allows explicit access to key and value (Java-Style iterator):

    QMap extensions;
    // ... fill extensions
    QMapIterator i(extensions);
    while (i.hasNext()) {
        i.next();
        qDebug() << i.key() << ": " << i.value();
    }
    

    In case you want to be able to overwrite, use QMutableMapIterator instead.

    There's another convenient Qt method, if you're only interested in reading the values, without the keys (using Qts foreach and c++11):

    QMap extensions;
    // ... fill extensions
    foreach (const auto& value, extensions)
    {
        // to stuff with value
    }
    

提交回复
热议问题