QMap Class

Detailed Description

QMap<Key, T> is one of Qt’s generic container classes. It stores (key, value) pairs and provides fast lookup by key.

QMap and QHash provide very similar functionality. The differences are:

  • QHash provides average faster lookups than QMap. (See Algorithmic Complexity for details.)
  • When iterating over a QHash, the items are arbitrarily ordered. With QMap, the items are always sorted by key.
  • The key type of a QHash must provide operator==() and a global qHash(Key) function. The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.

Here’s an example QMap with QString keys and int values:

To insert a (key, value) pair into the map, you can use operator[]():

This inserts the following three (key, value) pairs into the QMap: (“one”, 1), (“three”, 3), and (“seven”, 7). Another way to insert items into the map is to use insert():

To look up a value, use operator[]() or value():

If there is no item with the specified key in the map, these functions return a default-constructed value.

If you want to check whether the map contains a certain key, use contains():

There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:

In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a map. The reason is that operator[]() silently inserts an item into the map if no item exists with the same key (unless the map is const). For example, the following code snippet will create 1000 items in memory:

To avoid this problem, replace map[i] with map.value(i) in the code above.

If you want to navigate through all the (key, value) pairs stored in a QMap, you can use an iterator. QMap provides both Java-style iterators (QMapIterator and QMutableMapIterator) and STL-style iterators (QMap::const_iterator and QMap::iterator). Here’s how to iterate over a QMap<QString, int> using a Java-style iterator:

Here’s the same code, but using an STL-style iterator this time:

The items are traversed in ascending key order.

A QMap allows only one value per key. If you call insert() with a key that already exists in the QMap, the previous value will be erased. For example:

However, you can store multiple values per key by using QMultiMap.

If you only need to extract the values from a map (not the keys), you can also use range-based for:

Items can be removed from the map in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableMapIterator::remove(). In addition, you can clear the entire map using clear().

QMap’s key and value data types must be assignable data types. This covers most data types you are likely to encounter, but the compiler won’t let you, for example, store a QWidget as a value; instead, store a QWidget *. In addition, QMap’s key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equivalent if neither x < y nor y < x is true.

Example:

In the example, we start by comparing the employees’ names. If they’re equal, we compare their dates of birth to break the tie.