Iterating over a QMap with for

前端 未结 9 1705
执笔经年
执笔经年 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:57

    In "old" C++, using Qt, you would do it like this:

    QMap< QString, whatever > extensions;
    //...
    foreach( QString key, extensions.keys() )
    {
        fout << key << "," << extensions.value( key ) << '\n';
    }
    

    I don't have a C++11 compiler here but maybe the following will work:

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

    You can also use iterators instead, check out hmuelners link if you prefer using them

提交回复
热议问题