Search code examples
c++qtqtcore

Getting Java style Iterator from QMap<K, T> without specifying T, K


Is there any way obtaining java style iterator from QMap without specifying K and T explicitly ?

for example, writing

QMap<QString, SomeType> map;
auto qIt = map.getIterator();

Instead of

QMap<QString, SomeType> map;
QMapIterator<QString, SomeType> qIt(map);

Solution

  • If you're just trying to save on some repetitive typing then you could write a small function template to take advantage of the fact that the template parameters will be deduced from the passed arguments...

    template<typename Key, typename Value>
    QMapIterator<Key, Value> make_qiter (QMap<Key, Value> &map)
    {
      return(QMapIterator<Key, Value>(map));
    }
    

    Then use as...

    QMap<QString, SomeType> map;
    auto qIt = make_qiter(map);