Iterating over a QMap with for

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

    Ivan Čukić of KDAB has a blog post that explains how to iterate over a QMap with C++17 structured bindings without copying the container:

    template 
    class asKeyValueRange
    {
    public:
        asKeyValueRange(T &data) : m_data{data} {}
    
        auto begin() { return m_data.keyValueBegin(); }
    
        auto end() { return m_data.keyValueEnd(); }
    
    private:
        T &m_data;
    };
    
    ...
    
    QMap extensions;
    
    for (auto [key, value]: asKeyValueRange(extensions))
    {
        fout << key << ',' << value << '\n';
    } 
    

提交回复
热议问题