⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qhash.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    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(), QMultiHash::remove()*//*! \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(), QMultiHash::contains()*//*! \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(), value()*//*! \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(), keys()*//*!     \fn Key QHash::key(const T &value, const Key &defaultKey) const    \since 4.3    \overload    Returns the first key with value \a value, or \a defaultKey if    the hash contains no item with value \a value.    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 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*//*! \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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -