QMap Class

The QMap class is a template class that provides a red-black-tree-based dictionary. More...

Header: #include <QMap>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Types

class const_iterator
class iterator
class key_iterator
typedef ConstIterator
typedef Iterator
typedef const_key_value_iterator
typedef difference_type
typedef key_type
typedef key_value_iterator
typedef mapped_type
typedef size_type

Public Functions

QMap()
QMap(QMap<Key, T> &&other)
~QMap()
QMap::iterator begin()
QMap::const_iterator begin() const
QMap::const_iterator cbegin() const
QMap::const_iterator cend() const
void clear()
QMap::const_iterator constBegin() const
QMap::const_iterator constEnd() const
QMap::const_iterator constFind(const Key &key) const
QMap::const_key_value_iterator constKeyValueBegin() const
QMap::const_key_value_iterator constKeyValueEnd() const
bool contains(const Key &key) const
int count(const Key &key) const
int count() const
void detach()
bool empty() const
QMap::iterator end()
QMap::const_iterator end() const
int equal_range(const Key &)
int equal_range(const Key &) const
QMap::iterator erase(QMap::iterator it)
QMap::iterator find(const Key &key)
QMap::const_iterator find(const Key &key) const
T &first()
const T &first() const
const Key &firstKey() const
QMap::iterator insert(const Key &key, const T &value)
QMap::iterator insert(QMap::const_iterator pos, const Key &key, const T &value)
QMap::iterator insertMulti(const Key &key, const T &value)
QMap::iterator insertMulti(QMap::const_iterator pos, const Key &akey, const T &avalue)
bool isDetached() const
bool isEmpty() const
bool isSharedWith(const QMap<Key, T> &other) const
const Key key(const T &value, const Key &defaultKey = Key()) const
QMap::key_iterator keyBegin() const
QMap::key_iterator keyEnd() const
QMap::key_value_iterator keyValueBegin()
QMap::const_key_value_iterator keyValueBegin() const
QMap::key_value_iterator keyValueEnd()
QMap::const_key_value_iterator keyValueEnd() const
QList<Key> keys() const
QList<Key> keys(const T &value) const
T &last()
const T &last() const
const Key &lastKey() const
QMap::iterator lowerBound(const Key &key)
QMap::const_iterator lowerBound(const Key &key) const
int remove(const Key &key)
void setSharable(bool sharable)
int size() const
T take(const Key &key)
int toStdMap() const
QList<Key> uniqueKeys() const
QMap<Key, T> &unite(const QMap<Key, T> &other)
QMap::iterator upperBound(const Key &key)
QMap::const_iterator upperBound(const Key &key) const
const T value(const Key &key, const T &defaultValue = T()) const
QList<T> values() const
QList<T> values(const Key &key) const
bool operator!=(const QMap<Key, T> &other) const
QMap<Key, T> &operator=(const QMap<Key, T> &other)
bool operator==(const QMap<Key, T> &other) const
T &operator[](const Key &key)
const T operator[](const Key &key) const

Detailed Description

The QMap class is a template class that provides a red-black-tree-based dictionary.

QMap<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides fast lookup of the value associated with a 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:


  QMap<QString, int> map;

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


  map["one"] = 1;
  map["three"] = 3;
  map["seven"] = 7;

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():


  map.insert("twelve", 12);

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


  int num1 = map["thirteen"];
  int num2 = map.value("thirteen");

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():


  int timeout = 30;
  if (map.contains("TIMEOUT"))
      timeout = map.value("TIMEOUT");

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


  int timeout = map.value("TIMEOUT", 30);

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:


  // WRONG
  QMap<int, QWidget *> map;
  ...
  for (int i = 0; i < 1000; ++i) {
      if (map[i] == okButton)
          cout << "Found button at index " << i << endl;
  }

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:


  QMapIterator<QString, int> i(map);
  while (i.hasNext()) {
      i.next();
      cout << i.key() << ": " << i.value() << endl;
  }

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


  QMap<QString, int>::const_iterator i = map.constBegin();
  while (i != map.constEnd()) {
      cout << i.key() << ": " << i.value() << endl;
      ++i;
  }

The items are traversed in ascending key order.

Normally, 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:


  map.insert("plenty", 100);
  map.insert("plenty", 2000);
  // map.value("plenty") == 2000

However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiMap). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:


  QList<int> values = map.values("plenty");
  for (int i = 0; i < values.size(); ++i)
      cout << values.at(i) << endl;

The items that share the same key are available from most recently to least recently inserted. Another approach is to call find() to get the STL-style iterator for the first item with a key and iterate from there:


  QMap<QString, int>::iterator i = map.find("plenty");
  while (i != map.end() && i.key() == "plenty") {
      cout << i.value() << endl;
      ++i;
  }

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


  QMap<QString, int> map;
  ...
  foreach (int value, map)
      cout << value << endl;

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 equal if neither x < y nor y < x is true.

Example:


  #ifndef EMPLOYEE_H
  #define EMPLOYEE_H

  class Employee
  {
  public:
      Employee() {}
      Employee(const QString &name, const QDate &dateOfBirth);
      ...

  private:
      QString myName;
      QDate myDateOfBirth;
  };

  inline bool operator<(const Employee &e1, const Employee &e2)
  {
      if (e1.name() != e2.name())
          return e1.name() < e2.name();
      return e1.dateOfBirth() < e2.dateOfBirth();
  }

  #endif // EMPLOYEE_H

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

See also QMapIterator, QMutableMapIterator, QHash, and QSet.

Member Type Documentation

typedef QMap::ConstIterator

Qt-style synonym for QMap<Key, T>::const_iterator.

typedef QMap::Iterator

Qt-style synonym for QMap<Key, T>::iterator.

typedef QMap::const_key_value_iterator

The QMap::const_key_value_iterator typedef provides an STL-style iterator for QMap and QMultiMap.

QMap::const_key_value_iterator is essentially the same as QMap::const_iterator with the difference that operator*() returns a key/value pair instead of a value.

This typedef was introduced in Qt 5.10.

See also QKeyValueIterator.

typedef QMap::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

typedef QMap::key_type

Typedef for Key. Provided for STL compatibility.

typedef QMap::key_value_iterator

The QMap::key_value_iterator typedef provides an STL-style iterator for QMap and QMultiMap.

QMap::key_value_iterator is essentially the same as QMap::iterator with the difference that operator*() returns a key/value pair instead of a value.

This typedef was introduced in Qt 5.10.

See also QKeyValueIterator.

typedef QMap::mapped_type

Typedef for T. Provided for STL compatibility.

typedef QMap::size_type

Typedef for int. Provided for STL compatibility.

Member Function Documentation

QMap::QMap()

Default constructs an instance of QMap.

QMap::QMap(QMap<Key, T> &&other)

Default constructs an instance of QMap.

QMap::~QMap()

Destroys the instance of QMap.

QMap::iterator QMap::begin()

QMap::const_iterator QMap::begin() const

QMap::const_iterator QMap::cbegin() const

QMap::const_iterator QMap::cend() const

void QMap::clear()

QMap::const_iterator QMap::constBegin() const

QMap::const_iterator QMap::constEnd() const

QMap::const_iterator QMap::constFind(const Key &key) const

QMap::const_key_value_iterator QMap::constKeyValueBegin() const

QMap::const_key_value_iterator QMap::constKeyValueEnd() const

bool QMap::contains(const Key &key) const

int QMap::count(const Key &key) const

int QMap::count() const

void QMap::detach()

bool QMap::empty() const

QMap::iterator QMap::end()

QMap::const_iterator QMap::end() const

int QMap::equal_range(const Key &)

int QMap::equal_range(const Key &) const

QMap::iterator QMap::erase(QMap::iterator it)

QMap::iterator QMap::find(const Key &key)

QMap::const_iterator QMap::find(const Key &key) const

T &QMap::first()

const T &QMap::first() const

const Key &QMap::firstKey() const

QMap::iterator QMap::insert(const Key &key, const T &value)

QMap::iterator QMap::insert(QMap::const_iterator pos, const Key &key, const T &value)

QMap::iterator QMap::insertMulti(const Key &key, const T &value)

QMap::iterator QMap::insertMulti(QMap::const_iterator pos, const Key &akey, const T &avalue)

bool QMap::isDetached() const

bool QMap::isEmpty() const

bool QMap::isSharedWith(const QMap<Key, T> &other) const

const Key QMap::key(const T &value, const Key &defaultKey = Key()) const

QMap::key_iterator QMap::keyBegin() const

QMap::key_iterator QMap::keyEnd() const

QMap::key_value_iterator QMap::keyValueBegin()

QMap::const_key_value_iterator QMap::keyValueBegin() const

QMap::key_value_iterator QMap::keyValueEnd()

QMap::const_key_value_iterator QMap::keyValueEnd() const

QList<Key> QMap::keys() const

QList<Key> QMap::keys(const T &value) const

T &QMap::last()

const T &QMap::last() const

const Key &QMap::lastKey() const

QMap::iterator QMap::lowerBound(const Key &key)

QMap::const_iterator QMap::lowerBound(const Key &key) const

int QMap::remove(const Key &key)

void QMap::setSharable(bool sharable)

int QMap::size() const

T QMap::take(const Key &key)

int QMap::toStdMap() const

QList<Key> QMap::uniqueKeys() const

QMap<Key, T> &QMap::unite(const QMap<Key, T> &other)

QMap::iterator QMap::upperBound(const Key &key)

QMap::const_iterator QMap::upperBound(const Key &key) const

const T QMap::value(const Key &key, const T &defaultValue = T()) const

QList<T> QMap::values() const

QList<T> QMap::values(const Key &key) const

bool QMap::operator!=(const QMap<Key, T> &other) const

QMap<Key, T> &QMap::operator=(const QMap<Key, T> &other)

Copy-assignment operator.

bool QMap::operator==(const QMap<Key, T> &other) const

T &QMap::operator[](const Key &key)

const T QMap::operator[](const Key &key) const