QMap Class
template <typename K, typename V> class QMap
The QMap class is a template class that provides an ordered associative container. More…
| Header | #include <QMap> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QtPrivate::AssocContainer |
List of all members, including inherited members
Public Functions
| QMap() | |
| const K & | firstKey() const |
| const K & | lastKey() const |
| V & | first() |
| const V & | first() const |
| V & | last() |
| const V & | last() const |
| typename Base::iterator | lowerBound(const K &k) |
| typename Base::const_iterator | lowerBound(const K &k) const |
| typename Base::iterator | upperBound(const K &k) |
| typename Base::const_iterator | upperBound(const K &k) const |
| V | takeFirst() |
| std::map<K, V> | toStdMap() const & |
| std::map<K, V> | toStdMap() && |
Detailed Description
QMap<Key, T> stores key-value pairs sorted by key and looks up values in logarithmic time. Each key appears at most once; inserting an existing key replaces its value. Use QMultiMap to store several values under one key. The key type needs operator<.
QMap<QString, int> ages;
ages.insert("Alice", 30);
ages["Bob"] = 25;
for (auto it = ages.begin(); it != ages.end(); ++it)
qDebug() << it.key() << it.value();
The size, lookup, insertion, removal and iteration functions are shared with the other associative containers and behave as they do in Qt. keyValueBegin(), keyValueEnd() and asKeyValueRange() visit the key and value pairs, as described in QKeyValueIterator.
Differences from Qt
QMap is implemented on top of std::map and has no implicit sharing: copying a map copies all of its elements. Iterator invalidation follows std::map rather than Qt.
Member Function Documentation
QMap::QMap()
Constructs an empty map.
V & QMap::first()
Returns a reference to the value associated with the smallest key. The map must not be empty.
See also last() and firstKey().
const V & QMap::first() const
This is an overloaded function.
const K & QMap::firstKey() const
Returns a reference to the smallest key in the map. The map must not be empty.
See also lastKey() and first().
V & QMap::last()
Returns a reference to the value associated with the largest key. The map must not be empty.
See also first() and lastKey().
const V & QMap::last() const
This is an overloaded function.
const K & QMap::lastKey() const
Returns a reference to the largest key in the map. The map must not be empty.
See also firstKey() and last().
typename Base::iterator QMap::lowerBound(const K &k)
Returns an iterator to the first item whose key is not less than k, or end() if there is none.
See also upperBound().
typename Base::const_iterator QMap::lowerBound(const K &k) const
This is an overloaded function.
V QMap::takeFirst()
Removes the item with the smallest key and returns its value. The map must not be empty.
See also first().
std::map<K, V> QMap::toStdMap() const &
Returns a std::map with the items of the map.
std::map<K, V> QMap::toStdMap() &&
This is an overloaded function.
Moves the items out of the map.
This function was introduced in 6.0.
typename Base::iterator QMap::upperBound(const K &k)
Returns an iterator to the first item whose key is greater than k, or end() if there is none.
See also lowerBound().
typename Base::const_iterator QMap::upperBound(const K &k) const
This is an overloaded function.