📄 qhash.cpp
字号:
\code QHash<QString, int> hash; hash.insert("January", 1); hash.insert("February", 2); ... hash.insert("December", 12); QHash<QString, int>::const_iterator i; for (i = hash.constBegin(); i != hash.constEnd(); ++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. 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. If you need to keep iterators over a long period of time, we recommend that you use QMap rather than QHash. \sa QHash::iterator, QHashIterator*//*! \fn QHash::const_iterator::operator Node *() const \internal*//*! \fn QHash::const_iterator::const_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::constBegin() QHash::constEnd()*//*! \fn QHash::const_iterator::const_iterator(void *node) \internal*//*! \fn QHash::const_iterator::const_iterator(const iterator &other) Constructs a copy of \a other.*//*! \fn const Key &QHash::const_iterator::key() const Returns the current item's key. \sa value()*//*! \fn const T &QHash::const_iterator::value() const Returns the current item's value. \sa key(), operator*()*//*! \fn const T &QHash::const_iterator::operator*() const Returns the current item's value. Same as value(). \sa key()*//*! \fn const T *QHash::const_iterator::operator->() const Returns a pointer to the current item's value. \sa value()*//*! \fn bool QHash::const_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::const_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::const_iterator &QHash::const_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::const_iterator QHash::const_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::const_iterator &QHash::const_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::const_iterator QHash::const_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::const_iterator QHash::const_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::const_iterator QHash::const_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::const_iterator &QHash::const_iterator::operator+=(int j) Advances the iterator by \a j items. (If \a j is negative, the iterator goes backward.) This operation can be slow for large \a j values. \sa operator-=(), operator+()*//*! \fn QHash::const_iterator &QHash::const_iterator::operator-=(int j) Makes the iterator go back by \a j items. (If \a j is negative, the iterator goes forward.) This operation can be slow for large \a j values. \sa operator+=(), operator-()*//*! \fn uint qHash(char key) \relates QHash Returns the hash value for \a key.*//*! \fn uint qHash(uchar key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(signed char key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(ushort key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(short key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(uint key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(int key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(ulong key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(long key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(quint64 key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(qint64 key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(QChar key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(const QByteArray &key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(const QString &key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn uint qHash(const T *key) \relates QHash \overload Returns the hash value for \a key.*//*! \fn QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash) \relates QHash Writes the hash \a hash to stream \a out. This function requires the key and value types to implement \c operator<<(). \sa {Format of the QDataStream operators}*//*! \fn QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash) \relates QHash Reads a hash from stream \a in into \a hash. This function requires the key and value types to implement \c operator>>(). \sa {Format of the QDataStream operators}*//*! \class QMultiHash \brief The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes. \ingroup tools \ingroup shared \mainclass \reentrant QMultiHash\<Key, T\> is one of Qt's generic \l{container classes}. It inherits QHash and extends it with a few convenience functions that make it more suitable than QHash for storing multi-valued hashes. A multi-valued hash is a hash that allows multiple values with the same key; QHash normally doesn't allow that, unless you call QHash::insertMulti(). Because QMultiHash inherits QHash, all of QHash's functionality also applies to QMultiHash. For example, you can use isEmpty() to test whether the hash is empty, and you can traverse a QMultiHash using QHash's iterator classes (for example, QHashIterator). But in addition, it provides an insert() function that corresponds to QHash::insertMulti(), and a replace() function that corresponds to QHash::insert(). It also provides convenient operator+() and operator+=(). Example: \code QMultiHash<QString, int> hash1, hash2, hash3; hash1.insert("plenty", 100); hash1.insert("plenty", 2000); // hash1.size() == 2 hash2.insert("plenty", 5000); // hash2.size() == 1 hash3 = hash1 + hash2; // hash3.size() == 3 \endcode Unlike QHash, QMultiHash provides no operator[]. Use value() or replace() if you want to access the most recently inserted item with a certain key. If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>: \code QList<int> values = hash.values("plenty"); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl; \endcode The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find() to get the STL-style iterator for the first item with a key and iterate from there: \code QMultiHash<QString, int>::iterator i = hash.find("plenty"); while (i != hash.end() && i.key() == "plenty") { cout << i.value() << endl; ++i; } \endcode QMultiHash's key and value data types must be \l{assignable data types}. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, QMultiHash's key type must provide operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key's type. See the QHash documentation for details. \sa QHash, QHashIterator, QMutableHashIterator, QMultiMap*//*! \fn QMultiHash::QMultiHash() Constructs an empty hash.*//*! \fn QMultiHash::QMultiHash(const QHash<Key, T> &other) Constructs a copy of \a other (which can be a QHash or a QMultiHash). \sa operator=()*//*! \fn QMultiHash::iterator QMultiHash::replace(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 insert()*//*! \fn QMultiHash::iterator QMultiHash::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 same key in the hash, this function will simply create a new one. (This behavior is different from replace(), which overwrites the value of an existing item.) \sa replace()*//*! \fn QMultiHash &QMultiHash::operator+=(const QMultiHash &other) Inserts all the items in the \a other hash into this hash and returns a reference to this hash. \sa insert()*//*! \fn QMultiHash QMultiHash::operator+(const QMultiHash &other) const Returns a hash that contains all the items in this hash in addition to all the items in \a other. If a key is common to both hashes, the resulting hash will contain the key multiple times. \sa operator+=()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -