?? qhash.cpp
字號:
*//*! \fn QHash::const_iterator QHash::constEnd() const Returns a const \l{STL-style iterator} pointing to the imaginary item after the last item in the hash. \sa constBegin(), end()*//*! \fn QHash::iterator QHash::erase(iterator pos) Removes the (key, value) pair associated with the iterator \a pos from the hash, and returns an iterator to the next item in the hash. Unlike remove() and take(), this function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash. For example: \code QHash<QObject *, int> objectHash; ... QHash<QObject *, int>::iterator i = objectHash.find(obj); while (i != objectHash.end() && i.key() == obj) { if (i.value() == 0) { i = objectHash.erase(i); } else { ++i; } } \endcode \sa remove(), take(), find()*//*! \fn QHash::iterator QHash::find(const Key &key) Returns an iterator pointing to the item with key \a key in the hash. If the hash contains no item with key \a key, the function returns end(). If the hash 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 QHash<QString, int> hash; ... QHash<QString, int>::const_iterator i = hash.find("HDR"); while (i != hash.end() && i.key() == "HDR") { cout << i.value() << endl; ++i; } \endcode \sa value(), values()*//*! \fn QHash::const_iterator QHash::find(const Key &key) const \overload*//*! \fn QHash::iterator QHash::constFind(const Key &key) const \since 4.1 Returns an iterator pointing to the item with key \a key in the hash. If the hash contains no item with key \a key, the function returns constEnd(). \sa find()*//*! \fn QHash::iterator QHash::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 QHash::iterator QHash::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 hash, 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 QHash<Key, T> &QHash::unite(const QHash<Key, T> &other) Inserts all the items in the \a other hash into this hash. If a key is common to both hashes, the resulting hash will contain the key multiple times. \sa insertMulti()*//*! \fn bool QHash::empty() const This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the hash is empty; otherwise returns false.*//*! \typedef QHash::ConstIterator Qt-style synonym for QHash::const_iterator.*//*! \typedef QHash::Iterator Qt-style synonym for QHash::iterator.*//*! \typedef QHash::difference_type Typedef for ptrdiff_t. Provided for STL compatibility.*//*! \typedef QHash::key_type Typedef for Key. Provided for STL compatibility.*//*! \typedef QHash::mapped_type Typedef for T. Provided for STL compatibility.*//*! \typedef QHash::size_type Typedef for int. Provided for STL compatibility.*//*! \typedef QHash::iterator::difference_type \internal*//*! \typedef QHash::iterator::iterator_category \internal*//*! \typedef QHash::iterator::pointer \internal*//*! \typedef QHash::iterator::reference \internal*//*! \typedef QHash::iterator::value_type \internal*//*! \typedef QHash::const_iterator::difference_type \internal*//*! \typedef QHash::const_iterator::iterator_category \internal*//*! \typedef QHash::const_iterator::pointer \internal*//*! \typedef QHash::const_iterator::reference \internal*//*! \typedef QHash::const_iterator::value_type \internal*//*! \class QHash::iterator \brief The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash. QHash 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. QHash\<Key, T\>::iterator allows you to iterate over a QHash (or QMultiHash) and to modify the value (but not the key) associated with a particular key. If you want to iterate over a const QHash, you should use QHash::const_iterator. It is generally good practice to use QHash::const_iterator on a non-const QHash as well, unless you need to change the QHash through the iterator. Const iterators are slightly faster, and can improve code readability. The default QHash::iterator constructor creates an uninitialized iterator. You must initialize it using a QHash function like QHash::begin(), QHash::end(), or QHash::find() before you can start iterating. Here's a typical loop that prints all the (key, value) pairs stored in a hash: \code QHash<QString, int> hash; hash.insert("January", 1); hash.insert("February", 2); ... hash.insert("December", 12); QHash<QString, int>::iterator i; for (i = hash.begin(); i != hash.end(); ++i) cout << i.key() << ": " << i.value() << endl; \endcode Unlike QMap, which orders its items by key, QHash stores its items in an arbitrary order. The only guarantee is that items that share the same key (because they were inserted using QHash::insertMulti()) 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 QHash::iterator that we cannot do with a QHash::const_iterator. Here's an example that increments every value stored in the QHash by 2: \code QHash<QString, int>::iterator i; for (i = hash.begin(); i != hash.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 QHash<QString, int>::iterator i = hash.begin(); while (i != hash.end()) { if (i.key().startsWith("_")) i = hash.erase(i); else ++i; } \endcode The call to QHash::erase() removes the item pointed to by the iterator from the hash, and returns an iterator to the next item. Here's another way of removing an item while iterating: \code QHash<QString, int>::iterator i = hash.begin(); while (i != hash.end()) { QHash<QString, int>::iterator prev = i; ++i; if (prev.key().startsWith("_")) hash.erase(prev); } \endcode It might be tempting to write code like this: \code // WRONG while (i != hash.end()) { if (i.key().startsWith("_")) hash.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 hash. However, be aware that any modification performed directly on the QHash has the potential of dramatically changing the order in which the items are stored in the hash, as they might cause QHash to rehash its internal data structure. There is one notable exception: QHash::erase(). This function can safely be called while iterating, and won't affect the order of items in the hash. If you need to keep iterators over a long period of time, we recommend that you use QMap rather than QHash. \sa QHash::const_iterator, QMutableHashIterator*//*! \fn QHash::iterator::operator Node *() const \internal*//*! \fn QHash::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 QHash::begin() QHash::end()*//*! \fn QHash::iterator::iterator(void *node) \internal*//*! \fn const Key &QHash::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 QHash::erase() followed by QHash::insert() or QHash::insertMulti(). \sa value()*//*! \fn T &QHash::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 &QHash::iterator::operator*() const Returns a modifiable reference to the current item's value. Same as value(). \sa key()*//*! \fn T *QHash::iterator::operator->() const Returns a pointer to the current item's value. \sa value()*//*! \fn bool QHash::iterator::operator==(const iterator &other) const \fn bool QHash::iterator::operator==(const const_iterator &other) const Returns true if \a other points to the same item as this iterator; otherwise returns false. \sa operator!=()*//*! \fn bool QHash::iterator::operator!=(const iterator &other) const \fn bool QHash::iterator::operator!=(const const_iterator &other) const Returns true if \a other points to a different item than this iterator; otherwise returns false. \sa operator==()*//*! \fn QHash::iterator &QHash::iterator::operator++() The prefix ++ operator (\c{++i}) advances the iterator to the next item in the hash and returns an iterator to the new current item. Calling this function on QHash::end() leads to undefined results. \sa operator--()*//*! \fn QHash::iterator QHash::iterator::operator++(int) \overload The postfix ++ operator (\c{i++}) advances the iterator to the next item in the hash and returns an iterator to the previously current item.*//*! \fn QHash::iterator &QHash::iterator::operator--() The prefix -- operator (\c{--i}) makes the preceding item current and returns an iterator pointing to the new current item. Calling this function on QHash::begin() leads to undefined results. \sa operator++()*//*! \fn QHash::iterator QHash::iterator::operator--(int) \overload The postfix -- operator (\c{i--}) makes the preceding item current and returns an iterator pointing to the previously current item.*//*! \fn QHash::iterator QHash::iterator::operator+(int j) const Returns an iterator to the item at \a j positions forward from this iterator. (If \a j is negative, the iterator goes backward.) This operation can be slow for large \a j values. \sa operator-()*//*! \fn QHash::iterator QHash::iterator::operator-(int j) const Returns an iterator to the item at \a j positions backward from this iterator. (If \a j is negative, the iterator goes forward.) This operation can be slow for large \a j values. \sa operator+()*//*! \fn QHash::iterator &QHash::iterator::operator+=(int j) Advances the iterator by \a j items. (If \a j is negative, the iterator goes backward.) \sa operator-=(), operator+()*//*! \fn QHash::iterator &QHash::iterator::operator-=(int j) Makes the iterator go back by \a j items. (If \a j is negative, the iterator goes forward.) \sa operator+=(), operator-()*//*! \class QHash::const_iterator
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -