CopperSpice API  1.9.1
QHash< Key, Val, Hash, KeyEqual > Class Template Reference

Template class which provides an unordered container of keys and values. More...

Classes

class  const_iterator
 STL style const iterator for QHash More...
 
class  iterator
 STL style non-const iterator for QHash More...
 

Public Typedefs

using const_pointer = const Val *
 
using const_reference = const Val &
 
using difference_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::difference_type
 
using hasher = typename std::unordered_map< Key, Val, Hash, KeyEqual >::hasher
 
using key_equal = typename std::unordered_map< Key, Val, Hash, KeyEqual >::key_equal
 
using key_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::key_type
 
using mapped_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::mapped_type
 
using pointer = Val *
 
using reference = Val &
 
using size_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::difference_type
 
using value_type = Val
 

Public Methods

 QHash () = default
 
 QHash (const Hash &hash, const KeyEqual &key_equal=KeyEqual ())
 
 QHash (const QHash< Key, Val, Hash, KeyEqual > &other) = default
 
 QHash (const std::unordered_map< Key, Val, Hash, KeyEqual > &other)
 
template<typename Input_Iterator >
 QHash (Input_Iterator first, Input_Iterator last, const Hash &hash=Hash (), const KeyEqual &key_equal=KeyEqual ())
 
 QHash (QHash< Key, Val, Hash, KeyEqual > &&other) = default
 
 QHash (std::initializer_list< std::pair< const Key, Val >> list, const Hash &hash=Hash (), const KeyEqual &key_equal=KeyEqual ())
 
 QHash (std::unordered_map< Key, Val, Hash, KeyEqual > &&other)
 
 ~QHash () = default
 
iterator begin ()
 
const_iterator begin () const
 
size_type capacity () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_iterator constFind (const Key &key) const
 
bool contains (const Key &key) const
 
size_type count () const
 
size_type count (const Key &key) const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
QPair< iterator, iteratorequal_range (const Key &key)
 
QPair< const_iterator, const_iteratorequal_range (const Key &key) const
 
iterator erase (const_iterator iter)
 
iterator find (const Key &key)
 
const_iterator find (const Key &key) const
 
iterator insert (const Key &key, const Val &value)
 
iterator insert (const std::pair< const Key, Val > &data)
 
bool isEmpty () const
 
const Key key (const Val &value) const
 
const Key key (const Val &value, const Key &defaultKey) const
 
QList< Key > keys () const
 
QList< Key > keys (const Val &value) const
 
bool operator!= (const QHash< Key, Val, Hash, KeyEqual > &other) const
 
QHash< Key, Val, Hash, KeyEqual > & operator= (const QHash< Key, Val, Hash, KeyEqual > &other) = default
 
QHash< Key, Val, Hash, KeyEqual > & operator= (QHash< Key, Val, Hash, KeyEqual > &&other) = default
 
bool operator== (const QHash< Key, Val, Hash, KeyEqual > &other) const
 
Val & operator[] (const Key &key)
 
const Val operator[] (const Key &key) const
 
size_type remove (const Key &key)
 
void reserve (size_type size)
 
size_type size () const
 
void squeeze ()
 
void swap (QHash< Key, Val, Hash, KeyEqual > &other)
 
Val take (const Key &key)
 
QHash< Key, Val, Hash, KeyEqual > & unite (const QHash< Key, Val, Hash, KeyEqual > &other)
 
Val value (const Key &key) const
 
Val value (const Key &key, const Val &defaultValue) const
 
QList< Val > values () const
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &stream, const QHash< Key, Val, Hash, KeyEqual > &hash)
 
QDataStreamoperator>> (QDataStream &stream, QHash< Key, Val, Hash, KeyEqual > &hash)
 
uint qHash (char key)
 
uint qHash (const QBitArray &key)
 
uint qHash (const QByteArray &key)
 
uint qHash (const QHostAddress &key, uint seed=0)
 
uint qHash (const QItemSelectionRange &key, uint seed=0)
 
uint qHash (const QList< T > &key, uint seed=0)
 
uint qHash (const QPair< T1, T2 > &key)
 
uint qHash (const QSharedPointer< T > &ptr, uint seed=0)
 
uint qHash (const QSourceLocation &location)
 
uint qHash (const QSslError &key, uint seed)
 
uint qHash (const QString &key)
 
uint qHash (const QTransform &key, uint seed=0)
 
uint qHash (const QUrl &key, uint seed=0)
 
uint qHash (const QUrlQuery &key, uint seed=0)
 
uint qHash (const QVector< T > &key, uint seed=0)
 
uint qHash (const QXmlName &name)
 
uint qHash (const QXmlNodeModelIndex &index)
 
uint qHash (const T *key)
 
uint qHash (int key)
 
uint qHash (long key)
 
uint qHash (QChar key)
 
uint qHash (qint64 key)
 
uint qHash (QSslEllipticCurve curve, uint seed=0)
 
uint qHash (quint64 key)
 
uint qHash (short key)
 
uint qHash (signed char key)
 
uint qHash (uchar key)
 
uint qHash (uint key)
 
uint qHash (ulong key)
 
uint qHash (ushort key)
 

Detailed Description

template<typename Key, typename Val, typename Hash, typename KeyEqual>
class QHash< Key, Val, Hash, KeyEqual >

The QHash class is a template class which provides an unordered container of keys and values. This container stores the key and value as a pair. For a given key there is one corresponding value.

  • Elements in a QMap are sorted by key, elements in a QHash are in an arbitrary order/li>
  • QHash provides faster lookups than a QMap or a QFlatMap
  • The key data type for a QHash must implement the operator==() method and a global qHash(key) function
  • The key data type for a QMap must implement the operator<() method
  • Use QFlatMap when your data set is relatively small

For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.

Constructors

The following code is a simple example showing how to declare a QHash.

QHash<QString, int> hash; // stores QString key and int values

Accessing Elements

To insert a (key, value) pair into the hash use operator[]. This example inserts the following three (key, value) pairs into the QHash: ("one", 1), ("three", 3), and ("seven", 7).

hash["one"] = 1;
hash["three"] = 3;
hash["seven"] = 7;

Another way to insert items into the hash is to use the insert() method.

hash.insert("twelve", 12);

The following are examples to look up a value using the operator[] or value() methods.

int num1 = hash["thirteen"]; // if the key is not present ("thirteen", 0) will be inserted
int num2 = hash.value("thirteen");

If you want to check whether the hash contains a particular key use the method contains().

int timeout = 30;
if (hash.contains("TIMEOUT")) {
timeout = hash.value("TIMEOUT");
}

There is an overload for value() which accepts a second argument as a default value if there is no item with the specified key.

int timeout = hash.value("TIMEOUT", 30);

In general you should use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code will create 1000 items in memory.

// incorrect
for (int i = 0; i < 1000; ++i) {
if (hash[i] == okButton) {
cout << "Found button at index " << i << endl;
}
}

To avoid this problem, replace hash[i] with hash.value(i) in the code above.

Iterators

To navigate through all (key, value) pairs stored in a QHash use an iterator. QHash provides both Java style iterators (QHashIterator and QMutableHashIterator) and STL style iterators (QHash::const_iterator and QHash::iterator).

The following shows how to iterate over a QHash<QString, int> using a Java style iterator.

while (iter.hasNext()) {
iter.next();
cout << iter.key() << ": " << iter.value() << endl;
}

Here is the same code using an STL style iterator.

while (iter != hash.constEnd()) {
cout << iter.key() << ": " << iter.value() << endl;
++iter;
}

QHash is unordered so an iterator's sequence can not be assumed to be predictable. If ordering by key is required, use a QMap. QHash only allows only one value per key. To store multiple values for a given key use QMultiHash.

To extract the values from a hash and not the keys then use a range based.

for (int value : hash) {
cout << value << endl;
}

Items can be removed from the hash in several ways. One way is to call remove() which will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). To clear the entire hash use clear().

Constraints on type T

The QHash<Key, Val> should be Default Constructible and Copy Constructible to work with all methods of this class. Data types like QObject are not copy constructible and therefore should never be stored in a container. You can however use a pointer to a QObject or any subclass as the type T.

The QHash Key type support operator==().

There must also be a global qHash() function which returns a hash value for an argument of the key's type. The following is a list of data types which can serve as keys in a QHash. The <QHash> header file defines qHash() functions which compute hash values for the following data types.

Comparison Example

If you want to use other data types as the Key, you will need to ensure you provide operator==() and a qHash() implementation.

class Employee
{
public:
Employee() = default;
Employee(const QString &name, const QDate &dateOfBirth);
private:
QString myName;
QDate myDateOfBirth;
};
bool operator==(const Employee &e1, const Employee &e2) {
return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth();
}
uint qHash(const Employee &key) {
return qHash(key.name()) ^ key.dateOfBirth().day();
}

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 e1 == e2, then 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 the CopperSpice global qHash(const QString &) function is used to compute a hash value for the employee's name and it is combined with the day they were born. This will yield a different hash for people with the same name.

Internally, QHash uses a hash table to perform lookups. This hash table automatically grows and shrinks to provide fast lookups. You can control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain. This is not necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

See also
QMultiHash

Member Typedef Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::const_pointer

Typedef for const Val *.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::const_reference

Typedef for const Val &.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::difference_type

Typedef for integral type used to represent the distance between two elements.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::hasher

Typedef for the hash object type.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::key_equal

Typedef for the key equality comparison object type.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::key_type

Typedef for Key.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::mapped_type

Typedef for Val.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::pointer

Typedef for Val *.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::reference

Typedef for Val &.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::size_type

Typedef for a signed integer of the appropriate size for your platform.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::value_type

Typedef for Val.

Constructor & Destructor Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( )
default

Constructs an empty QHash.

See also
clear()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( const QHash< Key, Val, Hash, KeyEqual > &  other)
default

Copy constructs a new QHash from other.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( QHash< Key, Val, Hash, KeyEqual > &&  other)
default

Move constructs a new QHash from other.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( std::initializer_list< std::pair< const Key, Val >>  list,
const Hash &  hash = Hash(),
const KeyEqual &  key_equal = KeyEqual() 
)
inline

Constructs a new QHash with a copy of each of the elements in the initializer list.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( const Hash &  hash,
const KeyEqual &  key_equal = KeyEqual() 
)
inlineexplicit

Constructs a new QHash using the given hash and keyEqual objects.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( const std::unordered_map< Key, Val, Hash, KeyEqual > &  other)
inlineexplicit

Copy constructs a new QHash from other.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::QHash ( std::unordered_map< Key, Val, Hash, KeyEqual > &&  other)
inlineexplicit

Move constructs a new QHash from other.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
template<typename Input_Iterator >
QHash< Key, Val, Hash, KeyEqual >::QHash ( Input_Iterator  first,
Input_Iterator  last,
const Hash &  hash = Hash(),
const KeyEqual &  key_equal = KeyEqual() 
)
inline

Constructs a hash using the elements between the first and last iterators.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual >::~QHash ( )
default

Destroys this QHash.

Method Documentation

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::begin ( )
inline

Returns an STL style iterator pointing to the first item in the hash.

See also
constBegin(), end()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::begin ( ) const
inline

Returns a cont STL style iterator pointing to the first item in the hash.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
size_type QHash< Key, Val, Hash, KeyEqual >::capacity ( ) const
inline

Returns the number of buckets in the QHash's internal hash table.

The purpose of this method is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this method. If you want to know how many items are in the hash call size().

See also
reserve(), squeeze()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::cbegin ( ) const
inline

Returns a const STL style iterator pointing to the first item in the hash.

See also
begin(), cend()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::cend ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the hash.

See also
cbegin(), end()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QHash< Key, Val, Hash, KeyEqual >::clear ( )
inline

Removes all items from the hash.

See also
remove()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::constBegin ( ) const
inline

Returns a const STL style iterator pointing to the first item in the hash.

See also
begin(), constEnd()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::constEnd ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the hash.

See also
constBegin(), end()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::constFind ( const Key &  key) const
inline

Returns an iterator pointing to the item with the key in the hash. If the hash contains no item with the key the method returns constEnd().

See also
find()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::contains ( const Key &  key) const
inline

Returns true if the hash contains an item with the key, otherwise it returns false.

See also
count()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
size_type QHash< Key, Val, Hash, KeyEqual >::count ( ) const
inline

Returns the number of items in this hash. Equivalent to calling size().

template<typename Key , typename Val , typename Hash , typename KeyEqual >
size_type QHash< Key, Val, Hash, KeyEqual >::count ( const Key &  key) const
inline

Returns the number of items associated with the key.

See also
contains()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::empty ( ) const
inline

This method is identical to isEmpty(), returning true if the hash is empty otherwise returns false.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::end ( )
inline

Returns an STL style iterator pointing to the imaginary item after the last item in the hash.

See also
begin(), constEnd()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::end ( ) const
inline

Returns a const STL style iterator pointing to the imaginary item after the last item in the hash.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QPair< iterator, iterator > QHash< Key, Val, Hash, KeyEqual >::equal_range ( const Key &  key)
inline

Returns a pair of iterators corresponding to the range of items [first, second) which have the given key. If the range is empty then both iterators will be equal to end().

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QPair< const_iterator, const_iterator > QHash< Key, Val, Hash, KeyEqual >::equal_range ( const Key &  key) const
inline

Returns a pair of iterators corresponding to the range of items [first, second) which have the given key. If the range is empty then both iterators will be equal to end().

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::erase ( const_iterator  iter)
inline

Removes the (key, value) pair pointed to by the iterator iter from the hash and returns an iterator to the next item in the hash.

This method assumes the iterator is valid and refers to an element in the hash. It will not check if iter points past the end of the hash or is equal to the end() iterator. This method does not cause QHash to rehash its internal data structure. This means the method can safely be called while iterating and will not affect the order of items in the hash.

while (iter != hash.end() && iter.key() == obj) {
if (iter.value() == 0) {
iter = hash.erase(iter);
} else {
++iter;
}
}
See also
remove(), take(), find()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::find ( const Key &  key)
inline

Returns an iterator pointing to the item with the key in the hash. If the hash contains no item with the key, the method returns end().

If the hash contains multiple items with the key, this method returns an iterator which points to the most recently inserted value. The other values are accessible by incrementing the iterator. The following is code which iterates over all the items with the same key:

while (iter != hash.end() && iter.key() == "HDR") {
cout << iter.value() << endl;
++iter;
}
See also
value(), values()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const_iterator QHash< Key, Val, Hash, KeyEqual >::find ( const Key &  key) const
inline

Returns a const iterator pointing to the item with the key in the hash. If the hash contains no item with the key, the method returns end().

template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::insert ( const Key &  key,
const Val &  value 
)
inline

Inserts a new item with the key and value. If there is already an item with the key, the value for this item is replaced with value.

See also
QMultiHash::insert()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
iterator QHash< Key, Val, Hash, KeyEqual >::insert ( const std::pair< const Key, Val > &  data)
inline

Inserts a new item with key and value from data, which is an std::pair. If there is already an item with this key, the value for the item is replaced with value.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::isEmpty ( ) const
inline

Returns true if the hash contains no items, otherwise it returns false.

See also
size()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Key QHash< Key, Val, Hash, KeyEqual >::key ( const Val &  value) const

Returns the first key mapped to value. If no item with the given value is found then a new value initialized object of type Key is returned. Refer to default constructed elements for additional information.

This method can be slow (linear time) because the internal data structure is optimized for fast lookup by key, not by value.

See also
keys(), value()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Key QHash< Key, Val, Hash, KeyEqual >::key ( const Val &  value,
const Key &  defaultKey 
) const

Returns the key of the first item matching the given value. Otherwise, returns the given defaultKey.

This method can be slow (linear time) because the internal data structure is optimized for fast lookup by key, not by value.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QList< Key > QHash< Key, Val, Hash, KeyEqual >::keys ( ) const

Returns a list containing all the keys in the hash, in an arbitrary order. The order is guaranteed to be the same as that used by values().

See also
key(), values()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QList< Key > QHash< Key, Val, Hash, KeyEqual >::keys ( const Val &  value) const

Returns a list containing all the keys associated with value, in an arbitrary order.

This method can be slow (linear time), because the internal QHash data structure is optimized for fast lookup by key, not by value.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::operator!= ( const QHash< Key, Val, Hash, KeyEqual > &  other) const
inline

Returns true if other is not equal to this hash otherwise it returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This method requires the type T to support operator==().

See also
operator==()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual > & QHash< Key, Val, Hash, KeyEqual >::operator= ( const QHash< Key, Val, Hash, KeyEqual > &  other)
default

Copy assigns from other and returns a reference to this object.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual > & QHash< Key, Val, Hash, KeyEqual >::operator= ( QHash< Key, Val, Hash, KeyEqual > &&  other)
default

Move assigns from other and returns a reference to this object.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
bool QHash< Key, Val, Hash, KeyEqual >::operator== ( const QHash< Key, Val, Hash, KeyEqual > &  other) const
inline

Returns true if other is equal to this hash, otherwise it returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This method requires the type T to support operator==().

See also
operator!=()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val & QHash< Key, Val, Hash, KeyEqual >::operator[] ( const Key &  key)
inline

Returns a reference to the value for the element with the given key. If the key was not found then a new value initialized object of type Val with the given key is inserted. A reference to the new element will be returned. Refer to default constructed elements for additional information.

See also
insert(), value()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
const Val QHash< Key, Val, Hash, KeyEqual >::operator[] ( const Key &  key) const
inline

Equivalent to calling value().

template<typename Key , typename Val , typename Hash , typename KeyEqual >
size_type QHash< Key, Val, Hash, KeyEqual >::remove ( const Key &  key)
inline

Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key is not in the hash, or greater than 1 if insertMulti() has been used with the key.

See also
clear(), take()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QHash< Key, Val, Hash, KeyEqual >::reserve ( size_type  size)
inline

Ensures the internal hash table consists of at least size buckets. This method is useful for code that needs to build a huge hash and wants to avoid repeated reallocation.

hash.reserve(20000);
for (int i = 0; i < 20000; ++i) {
hash.insert(keys[i], values[i]);
}

Ideally, size should be slightly more than the maximum number of items expected in the hash. The size does not need to be a prime number. If size is underestimate, the QHash will resize automatically.

See also
squeeze(), capacity()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
size_type QHash< Key, Val, Hash, KeyEqual >::size ( ) const
inline

Returns the number of items in the hash.

See also
isEmpty(), count()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QHash< Key, Val, Hash, KeyEqual >::squeeze ( )
inline

Reduces the size of the internal hash table to save memory. The purpose of this method is to provide a way to fine tune memory usage. You will rarely need to call this method.

See also
capacity(), reserve()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
void QHash< Key, Val, Hash, KeyEqual >::swap ( QHash< Key, Val, Hash, KeyEqual > &  other)
inline

Swaps hash other with this hash. This operation is very fast and never fails.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val QHash< Key, Val, Hash, KeyEqual >::take ( const Key &  key)
inline

Removes the item with the given key and returns the value. If you do not use the return value remove() is more efficient. If the key was not found then a new value initialized object of type Val with the given key is returned. Refer to default constructed elements for additional information.

See also
remove()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
QHash< Key, Val, Hash, KeyEqual > & QHash< Key, Val, Hash, KeyEqual >::unite ( const QHash< Key, Val, Hash, KeyEqual > &  other)
inline

Inserts all the items from the other hash into this hash. If a key is common to both hashes, the value in this hash will be retained.

template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val QHash< Key, Val, Hash, KeyEqual >::value ( const Key &  key) const
inline

Returns the value associated with the given key. If no item with the given key is found, a new value initialized object of type Val with the given key is returned. Refer to default constructed elements for additional information.

See also
contains(), key(), values(), operator[]()
template<typename Key , typename Val , typename Hash , typename KeyEqual >
Val QHash< Key, Val, Hash, KeyEqual >::value ( const Key &  key,
const Val &  defaultValue 
) const
inline

Returns the value matching the given key. Otherwise, returns the defaultValue

template<typename Key , typename Val , typename Hash , typename KeyEqual >
QList< Val > QHash< Key, Val, Hash, KeyEqual >::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().

See also
keys(), value()

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream stream,
const QHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Writes the given hash to the stream. Returns a reference to the stream. Requires the type T to support operator<<().

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream stream,
QHash< Key, Val, Hash, KeyEqual > &  hash 
)
related

Reads from the stream into the given hash. Returns a reference to the stream. Requires the type T to support operator>>().

Refer to Serializing Data Types for additional information.

uint qHash ( char  key)
related

Returns the hash value for the key.

uint qHash ( const QBitArray key)
related

Returns the hash value for the key.

uint qHash ( const QByteArray key)
related

Returns the hash value for the key.

uint qHash ( const QHostAddress key,
uint  seed = 0 
)
related

Returns a hash of the host address key using seed to seed the calculation.

uint qHash ( const QItemSelectionRange key,
uint  seed = 0 
)
related

Returns the hash value for key using seed to seed the calculation.

uint qHash ( const QList< T > &  key,
uint  seed = 0 
)
related

Returns the hash value for key using seed to seed the calculation. Requires qHash() to be overloaded for the value type T.

uint qHash ( const QPair< T1, T2 > &  key)
related

Returns the hash value for key. Template types T1 and T2 must be supported by qHash().

uint qHash ( const QSharedPointer< T > &  ptr,
uint  seed = 0 
)
related

Returns a hash of the shared pointer ptr using seed to seed the calculation.

uint qHash ( const QSourceLocation location)
related

Computes a hash key for the QSourceLocation location.

uint qHash ( const QSslError key,
uint  seed 
)
related

Returns the hash value for the key using seed to seed the calculation.

uint qHash ( const QString key)
related

Returns the hash value for the key.

uint qHash ( const QTransform key,
uint  seed = 0 
)
related

Returns the hash value for key using seed to seed the calculation.

uint qHash ( const QUrl key,
uint  seed = 0 
)
related

Returns the hash value for key using seed to seed the calculation.

uint qHash ( const QUrlQuery key,
uint  seed = 0 
)
related

Returns the hash value for key using seed to seed the calculation.

uint qHash ( const QVector< T > &  key,
uint  seed = 0 
)
related

Returns the hash value for key, using seed to seed the calculation. Requires qHash() to be overloaded for the value type T.

uint qHash ( const QXmlName name)
related

Returns the hash value based on the local name and the namespace URI in name. The prefix in name is not used in the computation.

uint qHash ( const QXmlNodeModelIndex index)
related

Returns the hash value for index.

The hash is computed on QXmlNodeModelIndex::data(), QXmlNodeModelIndex::additionalData(), and QXmlNodeModelIndex::model(). This means the hash key can be used for node indexes from different node models.

uint qHash ( const T *  key)
related

Returns the hash value for the key.

uint qHash ( int  key)
related

Returns the hash value for the key.

uint qHash ( long  key)
related

Returns the hash value for the key.

uint qHash ( QChar  key)
related

Returns the hash value for the key.

uint qHash ( qint64  key)
related

Returns the hash value for the key.

uint qHash ( QSslEllipticCurve  curve,
uint  seed = 0 
)
related

Returns an hash value for the curve, using seed to seed the calculation.

uint qHash ( quint64  key)
related

Returns the hash value for the key.

uint qHash ( short  key)
related

Returns the hash value for the key.

uint qHash ( signed char  key)
related

Returns the hash value for the key.

uint qHash ( uchar  key)
related

Returns the hash value for the key.

uint qHash ( uint  key)
related

Returns the hash value for the key.

uint qHash ( ulong  key)
related

Returns the hash value for the key.

uint qHash ( ushort  key)
related

Returns the hash value for the key.