Search code examples
qtqmap

Nested QMap - How to insert without instantiating


QMap<QString,int> map;

QMap<int,QMap<QString,int>> table;

QMap<QString,int>::iterator iter = map.begin();
int i = 0;
while (iter != map.end()) 
{
   if (condition) {
      table.insert(i++,iter.key(),iter.value());  // <--- this is obviously wrong
   else
      ++iter;
}

So bascially I need to filter our data in map and insert them as new QMap as a value in the table QMap. How to go around with this?


Solution

  • Since Qt 5.1, when compiling as c++11, you could use an initializer list:

    table.insert( i++, QMap<QString,int> {{iter.key(),iter.value()}} );
    

    But the question title says "without instantiating", and that is not possible. This is just another constructor.