Iterating over a QMap with for

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

    C++11 range-based-for uses the type of the dereferenced iterator as the automatically deduced "cursor" type. Here, it is the type of the expression *map.begin().
    And since QMap::iterator::operator*() returns a reference to the value (of type QString &), the key isn't accessible using that method.

    You should use one of the iterator methods described in the documentation but you should avoid using

    • keys() because it involves creating a list of keys and then searching the value for each key, or,
    • toStdMap() because it copies all the map elements to another one,

    and that wouldn't be very optimal.


    You could also use a wrapper to get QMap::iterator as the auto type:

    template
    struct RangeWrapper {
        typedef typename Map::iterator MapIterator;
        Map ↦
    
        RangeWrapper(Map & map_) : map(map_) {}
    
        struct iterator {
            MapIterator mapIterator;
            iterator(const MapIterator &mapIterator_): mapIterator(mapIterator_) {}
            MapIterator operator*() {
                return mapIterator;
            }
            iterator & operator++() {
                ++mapIterator;
                return *this;
            }
            bool operator!=(const iterator & other) {
                return this->mapIterator != other.mapIterator;
            }
        };
        iterator begin() {
            return map.begin();
        }
        iterator end() {
            return map.end();
        }
    };
    
    // Function to be able to use automatic template type deduction
    template
    RangeWrapper toRange(Map & map)
    {
        return RangeWrapper(map);
    }
    
    // Usage code
    QMap extensions;
    ...
    for(auto e : toRange(extensions)) {
        fout << e.key() << "," << e.value() << '\n';
    }
    

    There is another wrapper here.

提交回复
热议问题