I\'ve a QMap
object and I am trying to write its content to a file.
QMap extensions;
//..
for(auto e : extensions)
{
fou
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.
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
There is another wrapper here.