Qt 4.8
Public Functions | Private Functions | List of all members
QMultiHash< Key, T > Class Template Reference

The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes. More...

#include <qcontainerfwd.h>

Inheritance diagram for QMultiHash< Key, T >:
QHash< Key, T >

Public Functions

QHash< Key, T >::const_iterator constFind (const Key &key, const T &value) const
 Returns an iterator pointing to the item with the key and the value in the hash. More...
 
bool contains (const Key &key, const T &value) const
 Returns true if the hash contains an item with the key and value; otherwise returns false. More...
 
int count (const Key &key, const T &value) const
 Returns the number of items with the key and value. More...
 
QHash< Key, T >::iterator find (const Key &key, const T &value)
 Returns an iterator pointing to the item with the key and value. More...
 
QHash< Key, T >::const_iterator find (const Key &key, const T &value) const
 
QHash< Key, T >::iterator insert (const Key &key, const T &value)
 Inserts a new item with the key and a value of value. More...
 
QMultiHash operator+ (const QMultiHash &other) const
 Returns a hash that contains all the items in this hash in addition to all the items in other. More...
 
QMultiHashoperator+= (const QMultiHash &other)
 Inserts all the items in the other hash into this hash and returns a reference to this hash. More...
 
 QMultiHash ()
 Constructs an empty hash. More...
 
 QMultiHash (const QHash< Key, T > &other)
 Constructs a copy of other (which can be a QHash or a QMultiHash). More...
 
int remove (const Key &key, const T &value)
 Removes all the items that have the key and the value value from the hash. More...
 
QHash< Key, T >::iterator replace (const Key &key, const T &value)
 Inserts a new item with the key and a value of value. More...
 
void swap (QMultiHash< Key, T > &other)
 Swaps hash other with this hash. More...
 
- Public Functions inherited from QHash< Key, T >
iterator begin ()
 Returns an STL-style iterator pointing to the first item in the hash. More...
 
const_iterator begin () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
int capacity () const
 Returns the number of buckets in the QHash's internal hash table. More...
 
void clear ()
 Removes all items from the hash. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first item in the hash. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash. More...
 
const_iterator constFind (const Key &key) const
 Returns an iterator pointing to the item with the key in the hash. More...
 
bool contains (const Key &key) const
 Returns true if the hash contains an item with the key; otherwise returns false. More...
 
int count (const Key &key) const
 Returns the number of items associated with the key. More...
 
int count () const
 Same as size(). More...
 
void detach ()
 Detaches this hash from any other hashes with which it may share data. More...
 
bool empty () const
 This function is provided for STL compatibility. More...
 
iterator end ()
 Returns an STL-style iterator pointing to the imaginary item after the last item in the hash. More...
 
const_iterator end () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator erase (iterator it)
 Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash. More...
 
iterator find (const Key &key)
 Returns an iterator pointing to the item with the key in the hash. More...
 
const_iterator find (const Key &key) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator insert (const Key &key, const T &value)
 Inserts a new item with the key and a value of value. More...
 
iterator insertMulti (const Key &key, const T &value)
 Inserts a new item with the key and a value of value. More...
 
bool isDetached () const
 Returns true if the hash's internal data isn't shared with any other hash object; otherwise returns false. More...
 
bool isEmpty () const
 Returns true if the hash contains no items; otherwise returns false. More...
 
bool isSharedWith (const QHash< Key, T > &other) const
 
const Key key (const T &value) const
 Returns the first key mapped to value. More...
 
const Key key (const T &value, const Key &defaultKey) const
 Returns the first key mapped to value, or defaultKey if the hash contains no item mapped to value. More...
 
QList< Key > keys () const
 Returns a list containing all the keys in the hash, in an arbitrary order. More...
 
QList< Key > keys (const T &value) const
 Returns a list containing all the keys associated with value value, in an arbitrary order. More...
 
bool operator!= (const QHash< Key, T > &other) const
 Returns true if other is not equal to this hash; otherwise returns false. More...
 
QHash< Key, T > & operator= (const QHash< Key, T > &other)
 Assigns other to this hash and returns a reference to this hash. More...
 
bool operator== (const QHash< Key, T > &other) const
 Returns true if other is equal to this hash; otherwise returns false. More...
 
T & operator[] (const Key &key)
 Returns the value associated with the key as a modifiable reference. More...
 
const T operator[] (const Key &key) const
 Same as value(). More...
 
 QHash ()
 Constructs an empty hash. More...
 
 QHash (const QHash< Key, T > &other)
 Constructs a copy of other. More...
 
int remove (const Key &key)
 Removes all the items that have the key from the hash. More...
 
void reserve (int size)
 Ensures that the QHash's internal hash table consists of at least size buckets. More...
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of items in the hash. More...
 
void squeeze ()
 Reduces the size of the QHash's internal hash table to save memory. More...
 
void swap (QHash< Key, T > &other)
 Swaps hash other with this hash. More...
 
take (const Key &key)
 Removes the item with the key from the hash and returns the value associated with it. More...
 
QList< Key > uniqueKeys () const
 Returns a list containing all the keys in the map. More...
 
QHash< Key, T > & unite (const QHash< Key, T > &other)
 Inserts all the items in the other hash into this hash. More...
 
const T value (const Key &key) const
 Returns the value associated with the key. More...
 
const T value (const Key &key, const T &defaultValue) const
 If the hash contains no item with the given key, the function returns defaultValue. More...
 
QList< T > values () const
 Returns a list containing all the values in the hash, in an arbitrary order. More...
 
QList< T > values (const Key &key) const
 Returns a list of all the values associated with the key, from the most recently inserted to the least recently inserted. More...
 
 ~QHash ()
 Destroys the hash. More...
 

Private Functions

T & operator[] (const Key &key)
 
const T operator[] (const Key &key) const
 

Additional Inherited Members

- Public Types inherited from QHash< Key, T >
typedef const_iterator ConstIterator
 Qt-style synonym for QHash::const_iterator. More...
 
typedef qptrdiff difference_type
 Typedef for ptrdiff_t. More...
 
typedef iterator Iterator
 Qt-style synonym for QHash::iterator. More...
 
typedef Key key_type
 Typedef for Key. More...
 
typedef T mapped_type
 Typedef for T. More...
 
typedef int size_type
 Typedef for int. More...
 

Detailed Description

template<class Key, class T>
class QMultiHash< Key, T >

The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.

Note
This class or function is reentrant.

QMultiHash<Key, T> is one of Qt's generic 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:

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

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>:

QList<int> values = hash.values("plenty");
for (int i = 0; i < values.size(); ++i)
cout << values.at(i) << endl;

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:

while (i != hash.end() && i.key() == "plenty") {
cout << i.value() << endl;
++i;
}

QMultiHash's key and value data types must be 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.

See also
QHash, QHashIterator, QMutableHashIterator, QMultiMap

Definition at line 58 of file qcontainerfwd.h.

Constructors and Destructors

◆ QMultiHash() [1/2]

template<class Key, class T>
QMultiHash< Key, T >::QMultiHash ( )
inline

Constructs an empty hash.

Definition at line 927 of file qhash.h.

927 {}

◆ QMultiHash() [2/2]

template<class Key, class T>
QMultiHash< Key, T >::QMultiHash ( const QHash< Key, T > &  other)
inline

Constructs a copy of other (which can be a QHash or a QMultiHash).

See also
operator=()

Definition at line 928 of file qhash.h.

928 : QHash<Key, T>(other) {}
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66

Functions

◆ constFind()

template<class Key, class T>
typename QHash< Key, T >::const_iterator QMultiHash< Key, T >::constFind ( const Key &  key,
const T &  value 
) const
inline

Returns an iterator pointing to the item with the key and the value in the hash.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.3

If the hash contains no such item, the function returns constEnd().

See also
QHash::constFind()
QHash::constFind()

Definition at line 992 of file qhash.h.

Referenced by QDBusConnectionPrivate::socketRead(), and QDBusConnectionPrivate::socketWrite().

993  { return find(key, value); }
QHash< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with the key and value.
Definition: qhash.h:972
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674

◆ contains()

template<class Key, class T>
Q_INLINE_TEMPLATE bool QMultiHash< Key, T >::contains ( const Key &  key,
const T &  value 
) const

Returns true if the hash contains an item with the key and value; otherwise returns false.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.3
See also
QHash::contains()
QHash::contains()

Definition at line 1000 of file qhash.h.

1001 {
1003 }
QHash< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with the key and the value in the hash.
Definition: qhash.h:992
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:469
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674

◆ count()

template<class Key, class T>
Q_INLINE_TEMPLATE int QMultiHash< Key, T >::count ( const Key &  key,
const T &  value 
) const

Returns the number of items with the key and value.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.3
See also
QHash::count()
QHash::count()

Definition at line 1023 of file qhash.h.

Referenced by QGraphicsAnchorLayoutPrivate::constraintsFromPaths().

1024 {
1025  int n = 0;
1028  while (i != end && i.key() == key) {
1029  if (i.value() == value)
1030  ++n;
1031  ++i;
1032  }
1033  return n;
1034 }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
QHash< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with the key and the value in the hash.
Definition: qhash.h:992
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674

◆ find() [1/2]

template<class Key, class T>
typename QHash< Key, T >::iterator QMultiHash< Key, T >::find ( const Key &  key,
const T &  value 
)
inline

Returns an iterator pointing to the item with the key and value.

Since
4.3

If the hash contains no such item, the function returns end().

If the hash contains multiple items with the key and value, the iterator returned points to the most recently inserted item.

See also
QHash::find()

Definition at line 972 of file qhash.h.

Referenced by QMetaObject::changeGuard(), QObjectPrivate::clearGuards(), QDBusConnectionPrivate::connectRelay(), QDBusConnectionPrivate::connectSignal(), QDBusConnectionPrivate::disconnectRelay(), QDBusConnectionPrivate::disconnectSignal(), QDBusConnectionPrivate::handleSignal(), QTextFormatCollection::hasFormatCached(), QTextFormatCollection::indexForFormat(), qDBusRealToggleWatch(), qDBusRemoveWatch(), and QMetaObject::removeGuard().

972  {
973  typename QHash<Key, T>::iterator i(find(key));
974  typename QHash<Key, T>::iterator end(this->end());
975  while (i != end && i.key() == key) {
976  if (i.value() == value)
977  return i;
978  ++i;
979  }
980  return end;
981  }
QHash< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with the key and value.
Definition: qhash.h:972
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330

◆ find() [2/2]

template<class Key, class T>
typename QHash< Key, T >::const_iterator QMultiHash< Key, T >::find ( const Key &  key,
const T &  value 
) const
inline
Since
4.3 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 982 of file qhash.h.

982  {
985  while (i != end && i.key() == key) {
986  if (i.value() == value)
987  return i;
988  ++i;
989  }
990  return end;
991  }
The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash...
Definition: qhash.h:395
QHash< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with the key and the value in the hash.
Definition: qhash.h:992
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674

◆ insert()

template<class Key, class T>
QMultiHash::iterator QMultiHash< Key, T >::insert ( const Key &  key,
const T &  value 
)
inline

Inserts a new item with the key and a value of 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.)

See also
replace()

Definition at line 934 of file qhash.h.

Referenced by QMetaObject::changeGuard(), QIcdEngine::doRequestUpdate(), QGraphicsAnchorLayoutPrivate::findPaths(), and QDBusConnectionPrivate::setConnection().

935  { return QHash<Key, T>::insertMulti(key, value); }
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674
iterator insertMulti(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:772

◆ operator+()

template<class Key, class T>
QMultiHash QMultiHash< Key, T >::operator+ ( const QMultiHash< Key, T > &  other) const
inline

Returns a hash that contains all the items in this hash in addition to all the items in other.

If a key is common to both hashes, the resulting hash will contain the key multiple times.

See also
operator+=()

Definition at line 939 of file qhash.h.

940  { QMultiHash result = *this; result += other; return result; }
The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.
Definition: qcontainerfwd.h:58

◆ operator+=()

template<class Key, class T>
QMultiHash & QMultiHash< Key, T >::operator+= ( const QMultiHash< Key, T > &  other)
inline

Inserts all the items in the other hash into this hash and returns a reference to this hash.

See also
insert()

Definition at line 937 of file qhash.h.

938  { this->unite(other); return *this; }
QHash< Key, T > & unite(const QHash< Key, T > &other)
Inserts all the items in the other hash into this hash.
Definition: qhash.h:556

◆ operator[]() [1/2]

template<class Key, class T>
T& QMultiHash< Key, T >::operator[] ( const Key &  key)
private

◆ operator[]() [2/2]

template<class Key, class T>
const T QMultiHash< Key, T >::operator[] ( const Key &  key) const
private

◆ remove()

template<class Key, class T>
Q_INLINE_TEMPLATE int QMultiHash< Key, T >::remove ( const Key &  key,
const T &  value 
)

Removes all the items that have the key and the value value from the hash.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.3

Returns the number of items removed.

See also
QHash::remove()
QHash::remove()

Definition at line 1006 of file qhash.h.

Referenced by QIcdEngine::doRequestUpdate().

1007 {
1008  int n = 0;
1009  typename QHash<Key, T>::iterator i(find(key));
1011  while (i != end && i.key() == key) {
1012  if (i.value() == value) {
1013  i = this->erase(i);
1014  ++n;
1015  } else {
1016  ++i;
1017  }
1018  }
1019  return n;
1020 }
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QHash< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with the key and value.
Definition: qhash.h:972
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
const Key & key() const
Returns the current item&#39;s key as a const reference.
Definition: qhash.h:347
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
iterator erase(iterator it)
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator...
Definition: qhash.h:827

◆ replace()

template<class Key, class T>
QMultiHash::iterator QMultiHash< Key, T >::replace ( const Key &  key,
const T &  value 
)
inline

Inserts a new item with the key and a value of value.

If there is already an item with the key, that item's value is replaced with value.

If there are multiple items with the key, the most recently inserted item's value is replaced with value.

See also
insert()

Definition at line 931 of file qhash.h.

932  { return QHash<Key, T>::insert(key, value); }
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
const Key key(const T &value) const
Returns the first key mapped to value.
Definition: qhash.h:674

◆ swap()

template<class Key, class T>
void QMultiHash< Key, T >::swap ( QMultiHash< Key, T > &  other)
inline

Swaps hash other with this hash.

Since
4.8

This operation is very fast and never fails.

Definition at line 929 of file qhash.h.

929 { QHash<Key, T>::swap(other); } // prevent QMultiHash<->QHash swaps
void swap(QHash< Key, T > &other)
Swaps hash other with this hash.
Definition: qhash.h:290

The documentation for this class was generated from the following files: