?? qhash.cpp
字號:
\endcode However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiHash). 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 iterator for the first item with a key and iterate from there: \code QHash<QString, int>::iterator i = hash.find("plenty"); while (i != hash.end() && i.key() == "plenty") { cout << i.value() << endl; ++i; } \endcode If you only need to extract the values from a hash (not the keys), you can also use \l{foreach}: \code QHash<QString, int> hash; ... foreach (int value, hash) cout << value << endl; \endcode Items can be removed from the hash in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). In addition, you can clear the entire hash using clear(). QHash'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, QHash'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. Here's a list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the \c <QHash> header defines a qHash() function that computes an adequate hash value. If you want to use other types as the key, make sure that you provide operator==() and a qHash() implementation. Example: \code #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) { return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth(); } inline uint qHash(const Employee &key) { return qHash(key.name()) ^ key.dateOfBirth().day(); } #endif // EMPLOYEE_H \endcode The qHash() function computes a numeric value based on a key. It can use any algorithm imaginable, as long as it always returns the same value if given the same argument. In other words, if \c{e1 == e2}, then \c{qHash(e1) == qHash(e2)} must hold as well. However, to obtain good performance, the qHash() function should attempt to return different hash values for different keys to the largest extent possible. In the example above, we've relied on Qt's global qHash(const QString &) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name. Internally, QHash uses a hash table to perform lookups. Unlike Qt 3's \c QDict class, which needed to be initialized with a prime number, QHash's hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size. \sa QHashIterator, QMutableHashIterator, QMap, QSet*//*! \fn QHash::QHash() Constructs an empty hash. \sa clear()*//*! \fn QHash::QHash(const QHash<Key, T> &other) Constructs a copy of \a other. This operation occurs in \l{constant time}, because QHash is \l{implicitly shared}. This makes returning a QHash from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes \l{linear time}. \sa operator=()*//*! \fn QHash::~QHash() Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.*//*! \fn QHash<Key, T> &QHash::operator=(const QHash<Key, T> &other) Assigns \a other to this hash and returns a reference to this hash.*//*! \fn bool QHash::operator==(const QHash<Key, T> &other) const Returns true if \a other is equal to this hash; otherwise returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This function requires the value type to implement \c operator==(). \sa operator!=()*//*! \fn bool QHash::operator!=(const QHash<Key, T> &other) const Returns true if \a other is not equal to this hash; otherwise returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This function requires the value type to implement \c operator==(). \sa operator==()*//*! \fn int QHash::size() const Returns the number of items in the hash. \sa isEmpty(), count()*//*! \fn bool QHash::isEmpty() const Returns true if the hash contains no items; otherwise returns false. \sa size()*//*! \fn int QHash::capacity() const Returns the number of buckets in the QHash's internal hash table. The sole purpose of this function is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the hash, call size(). \sa reserve(), squeeze()*//*! \fn void QHash::reserve(int size) Ensures that the QHash's internal hash table consists of at least \a size buckets. This function is useful for code that needs to build a huge hash and wants to avoid repeated reallocation. For example: \code QHash<QString, int> hash; hash.reserve(20000); for (int i = 0; i < 20000; ++i) hash.insert(keys[i], values[i]); \endcode Ideally, \a size should be slightly more than the maximum number of items expected in the hash. \a size doesn't have to be prime, because QHash will use a prime number internally anyway. If \a size is an underestimate, the worst that will happen is that the QHash will be a bit slower. In general, you will rarely ever need to call this function. QHash's internal hash table automatically shrinks or grows to provide good performance without wasting too much memory. \sa squeeze(), capacity()*//*! \fn void QHash::squeeze() Reduces the size of the QHash's internal hash table to save memory. The sole purpose of this function is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this function. \sa reserve(), capacity()*//*! \fn void QHash::detach() \internal Detaches this hash from any other hashes with which it may share data. \sa isDetached()*//*! \fn bool QHash::isDetached() const \internal Returns true if the hash's internal data isn't shared with any other hash object; otherwise returns false. \sa detach()*//*! \fn void QHash::setSharable(bool sharable) \internal*//*! \fn void QHash::clear() Removes all items from the hash. \sa remove()*//*! \fn int QHash::remove(const Key &key) Removes all the items that have the key \a key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn't in the hash, or greater than 1 if insertMulti() has been used with the \a key. \sa clear(), take()*//*! \fn T QHash::take(const Key &key) Removes the item with the key \a key from the hash and returns the value associated with it. If the item does not exist in the hash, the function simply returns a \l{default-constructed value}. If there are multiple items for \a key in the hash, only the most recently inserted one is removed. If you don't use the return value, remove() is more efficient. \sa remove()*//*! \fn bool QHash::contains(const Key &key) const Returns true if the hash contains an item with key \a key; otherwise returns false. \sa count()*//*! \fn const T QHash::value(const Key &key) const Returns the value associated with the key \a key. If the hash 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 hash, the value of the most recently inserted one is returned. \sa key(), values(), contains(), operator[]()*//*! \fn const T QHash::value(const Key &key, const T &defaultValue) const \overload If the hash contains no item with the given \a key, the function returns \a defaultValue.*//*! \fn T &QHash::operator[](const Key &key) Returns the value associated with the key \a key as a modifiable reference. If the hash contains no item with key \a key, the function inserts a \l{default-constructed value} into the hash with key \a key, and returns a reference to it. If the hash contains multiple items with key \a key, this function returns a reference to the most recently inserted value. \sa insert(), value()*//*! \fn const T QHash::operator[](const Key &key) const \overload Same as value().*//*! \fn QList<Key> QHash::uniqueKeys() const \since 4.2 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) occur only once in the returned list. \sa keys(), values()*//*! \fn QList<Key> QHash::keys() const Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (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 uniqueKeys(). The order is guaranteed to be the same as that used by values(). \sa uniqueKeys(), values(), key()*//*! \fn QList<Key> QHash::keys(const T &value) const \overload Returns a list containing all the keys associated with value \a value, in an arbitrary order. This function can be slow (\l{linear time}), because QHash's internal data structure is optimized for fast lookup by key, not by value.*//*! \fn QList<T> QHash::values() const Returns a list containing all the values in the hash, in an arbitrary order. If a key is associated multiple values, all of its values will be in the list, and not just the most recently inserted one. The order is guaranteed to be the same as that used by keys(). \sa keys()*//*! \fn QList<T> QHash::values(const Key &key) const \overload Returns a list of all the values associated with key \a key, from the most recently inserted to the least recently inserted. \sa count(), insertMulti()*//*! \fn Key QHash::key(const T &value) const Returns the first key with value \a value. If the hash 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 QHash's internal data structure is optimized for fast lookup by key, not by value. \sa value(), values()*//*! \fn int QHash::count(const Key &key) const Returns the number of items associated with key \a key. \sa contains(), insertMulti()*//*! \fn int QHash::count() const \overload Same as size().*//*! \fn QHash::iterator QHash::begin() Returns an \l{STL-style iterator} pointing to the first item in the hash. \sa constBegin(), end()*//*! \fn QHash::const_iterator QHash::begin() const \overload*//*! \fn QHash::const_iterator QHash::constBegin() const Returns a const \l{STL-style iterator} pointing to the first item in the hash. \sa begin(), constEnd()*//*! \fn QHash::iterator QHash::end() Returns an \l{STL-style iterator} pointing to the imaginary item after the last item in the hash. \sa begin(), constEnd()*//*! \fn QHash::const_iterator QHash::end() const \overload
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -