📄 qmap.cpp
字号:
\sa count()*//*! \fn const T QMap::value(const Key &key) const Returns the value associated with the key \a key. If the map contains no item with key \a key, the function returns a \l{default-constructed value}. If there are multiple items for \a key in the map, the value of the most recently inserted one is returned. \sa key(), values(), contains(), operator[]()*//*! \fn const T QMap::value(const Key &key, const T &defaultValue) const \overload If the map contains no item with key \a key, the function returns \a defaultValue.*//*! \fn T &QMap::operator[](const Key &key) Returns the value associated with the key \a key as a modifiable reference. If the map contains no item with key \a key, the function inserts a \l{default-constructed value} into the map with key \a key, and returns a reference to it. If the map contains multiple items with key \a key, this function returns a reference to the most recently inserted value. \sa insert(), value()*//*! \fn const T QMap::operator[](const Key &key) const \overload Same as value().*//*! \fn QList<Key> QMap::keys() const Returns a list containing all the keys in the map in ascending order. Keys that occur multiple times in the map (because items were inserted with insertMulti(), or unite() was used), also occur multiple times in the list. To obtain a list of unique keys, where each key from the map only occurs once, use an intermediate QSet object to filter out duplicates. The order is guaranteed to be the same as that used by values(). \sa values(), key()*//*! \fn QList<Key> QMap::keys(const T &value) const \overload Returns a list containing all the keys associated with value \a value in ascending order. This function can be slow (\l{linear time}), because QMap's internal data structure is optimized for fast lookup by key, not by value.*//*! \fn Key QMap::key(const T &value) const Returns the first key with value \a value. If the map contains no item with value \a value, the function returns a \link {default-constructed value} default-constructed key \endlink. This function can be slow (\l{linear time}), because QMap's internal data structure is optimized for fast lookup by key, not by value. \sa value(), values()*//*! \fn QList<T> QMap::values() const Returns a list containing all the values in the map, in ascending order of their keys. If a key is associated multiple values, all of its values will be in the list, and not just the most recently inserted one. \sa keys()*//*! \fn QList<T> QMap::values(const Key &key) const \overload Returns a list containing all the values associated with key \a key, from the most recently inserted to the least recently inserted one. \sa count(), insertMulti()*//*! \fn int QMap::count(const Key &key) const Returns the number of items associated with key \a key. \sa contains(), insertMulti()*//*! \fn int QMap::count() const \overload Same as size().*//*! \fn QMap::iterator QMap::begin() Returns an \l{STL-style iterator} pointing to the first item in the map. \sa constBegin(), end()*//*! \fn QMap::const_iterator QMap::begin() const \overload*//*! \fn QMap::const_iterator QMap::constBegin() const Returns a const \l{STL-style iterator} pointing to the first item in the map. \sa begin(), constEnd()*//*! \fn QMap::iterator QMap::end() Returns an \l{STL-style iterator} pointing to the imaginary item after the last item in the map. \sa begin(), constEnd()*//*! \fn QMap::const_iterator QMap::end() const \overload*//*! \fn QMap::const_iterator QMap::constEnd() const Returns a const \l{STL-style iterator} pointing to the imaginary item after the last item in the map. \sa constBegin(), end()*//*! \fn QMap::iterator QMap::erase(iterator pos) Removes the (key, value) pair pointed to by the iterator \a pos from the map, and returns an iterator to the next item in the map. \sa remove()*//*! \fn QMap::iterator QMap::find(const Key &key) Returns an iterator pointing to the item with key \a key in the map. If the map contains no item with key \a key, the function returns end(). If the map contains multiple items with key \a key, this function returns an iterator that points to the most recently inserted value. The other values are accessible by incrementing the iterator. For example, here's some code that iterates over all the items with the same key: \code QMap<QString, int> map; ... QMap<QString, int>::const_iterator i = map.find("HDR"); while (i != map.end() && i.key() == "HDR") { cout << i.value() << endl; ++i; } \endcode \sa constFind(), value(), values(), lowerBound(), upperBound()*//*! \fn QMap::const_iterator QMap::find(const Key &key) const \overload*//*! \fn QMap::iterator QMap::constFind(const Key &key) const \since 4.1 Returns an const iterator pointing to the item with key \a key in the map. If the map contains no item with key \a key, the function returns constEnd(). \sa find()*//*! \fn QMap::iterator QMap::lowerBound(const Key &key) Returns an iterator pointing to the first item with key \a key in the map. If the map contains no item with key \a key, the function returns an iterator to the nearest item with a greater key. Example: \code QMap<int, QString> map; map.insert(1, "one"); map.insert(5, "five"); map.insert(10, "ten"); map.lowerBound(0); // returns iterator to (1, "one") map.lowerBound(1); // returns iterator to (1, "one") map.lowerBound(2); // returns iterator to (5, "five") map.lowerBound(10); // returns iterator to (10, "ten") map.lowerBound(999); // returns end() \endcode If the map contains multiple items with key \a key, this function returns an iterator that points to the most recently inserted value. The other values are accessible by incrementing the iterator. For example, here's some code that iterates over all the items with the same key: \code QMap<QString, int> map; ... QMap<QString, int>::const_iterator i = map.lowerBound("HDR"); QMap<QString, int>::const_iterator upperBound = map.upperBound("HDR"); while (i != upperBound) { cout << i.value() << endl; ++i; } \endcode \sa qLowerBound(), upperBound(), find()*//*! \fn QMap::const_iterator QMap::lowerBound(const Key &key) const \overload*//*! \fn QMap::iterator QMap::upperBound(const Key &key) Returns an iterator pointing to the item that immediately follows the last item with key \a key in the map. If the map contains no item with key \a key, the function returns an iterator to the nearest item with a greater key. Example: \code QMap<int, QString> map; map.insert(1, "one"); map.insert(5, "five"); map.insert(10, "ten"); map.upperBound(0); // returns iterator to (1, "one") map.upperBound(1); // returns iterator to (5, "five") map.upperBound(2); // returns iterator to (5, "five") map.upperBound(10); // returns end() map.upperBound(999); // returns end() \endcode \sa qUpperBound(), lowerBound(), find()*//*! \fn QMap::const_iterator QMap::upperBound(const Key &key) const \overload*//*! \fn QMap::iterator QMap::insert(const Key &key, const T &value) Inserts a new item with the key \a key and a value of \a value. If there is already an item with the key \a key, that item's value is replaced with \a value. If there are multiple items with the key \a key, the most recently inserted item's value is replaced with \a value. \sa insertMulti()*//*! \fn QMap::iterator QMap::insertMulti(const Key &key, const T &value) Inserts a new item with the key \a key and a value of \a value. If there is already an item with the same key in the map, this function will simply create a new one. (This behavior is different from insert(), which overwrites the value of an existing item.) \sa insert(), values()*//*! \fn QMap<Key, T> &QMap::unite(const QMap<Key, T> &other) Inserts all the items in the \a other map into this map. If a key is common to both maps, the resulting map will contain the key multiple times. \sa insertMulti()*//*! \typedef QMap::Iterator Qt-style synonym for QMap::iterator.*//*! \typedef QMap::ConstIterator Qt-style synonym for QMap::const_iterator.*//*! \fn bool QMap::empty() const This function is provided for STL compatibility. It is equivalent to isEmpty().*//*! \class QMap::iterator \brief The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap. QMap features both \l{STL-style iterators} and \l{Java-style iterators}. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity. QMap\<Key, T\>::iterator allows you to iterate over a QMap (or QMultiMap) and to modify the value (but not the key) stored under a particular key. If you want to iterate over a const QMap, you should use QMap::const_iterator. It is generally good practice to use QMap::const_iterator on a non-const QMap as well, unless you need to change the QMap through the iterator. Const iterators are slightly faster, and can improve code readability. The default QMap::iterator constructor creates an uninitialized iterator. You must initialize it using a QMap function like QMap::begin(), QMap::end(), or QMap::find() before you can start iterating. Here's a typical loop that prints all the (key, value) pairs stored in a map: \code QMap<QString, int> map; map.insert("January", 1); map.insert("February", 2); ... map.insert("December", 12); QMap<QString, int>::iterator i; for (i = map.begin(); i != map.end(); ++i) cout << i.key() << ": " << i.value() << endl; \endcode Unlike QHash, which stores its items in an arbitrary order, QMap stores its items ordered by key. Items that share the same key (because they were inserted using QMap::insertMulti(), or due to a unite()) will appear consecutively, from the most recently to the least recently inserted value. Let's see a few examples of things we can do with a QMap::iterator that we cannot do with a QMap::const_iterator. Here's an example that increments every value stored in the QMap by 2: \code QMap<QString, int>::iterator i; for (i = map.begin(); i != map.end(); ++i) i.value() += 2; \endcode Here's an example that removes all the items whose key is a string that starts with an underscore character: \code QMap<QString, int>::iterator i = map.begin(); while (i != map.end()) { if (i.key().startsWith("_")) i = map.erase(i); else ++i; } \endcode The call to QMap::erase() removes the item pointed to by the iterator from the map, and returns an iterator to the next item. Here's another way of removing an item while iterating: \code QMap<QString, int>::iterator i = map.begin(); while (i != map.end()) { QMap<QString, int>::iterator prev = i; ++i; if (prev.key().startsWith("_")) map.erase(prev); } \endcode It might be tempting to write code like this: \code // WRONG while (i != map.end()) { if (i.key().startsWith("_")) map.erase(i); ++i; } \endcode However, this will potentially crash in \c{++i}, because \c i is a dangling iterator after the call to erase(). Multiple iterators can be used on the same map. If you add items to the map, existing iterators will remain valid. If you remove items from the map, iterators that point to the removed items will become dangling iterators. \sa QMap::const_iterator, QMutableMapIterator*//*! \fn QMap::iterator::operator QMapData::Node *() const \internal*//*! \typedef QMap::iterator::difference_type \internal*//*! \typedef QMap::iterator::iterator_category \internal*//*! \typedef QMap::iterator::pointer \internal*//*! \typedef QMap::iterator::reference \internal*//*! \typedef QMap::iterator::value_type \internal*//*! \fn QMap::iterator::iterator() Constructs an uninitialized iterator. Functions like key(), value(), and operator++() must not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it. \sa QMap::begin() QMap::end()*//*! \fn QMap::iterator::iterator(QMapData::Node *node) \internal*//*! \fn const Key &QMap::iterator::key() const Returns the current item's key as a const reference. There is no direct way of changing an item's key through an iterator, although it can be done by calling QMap::erase() followed by QMap::insert() or QMap::insertMulti(). \sa value()*//*! \fn T &QMap::iterator::value() const Returns a modifiable reference to the current item's value. You can change the value of an item by using value() on the left side of an assignment, for example: \code if (i.key() == "Hello") i.value() = "Bonjour"; \endcode \sa key(), operator*()*//*! \fn T &QMap::iterator::operator*() const Returns a modifiable reference to the current item's value. Same as value(). \sa key()*//*! \fn T *QMap::iterator::operator->() const Returns a pointer to the current item's value.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -