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

The QMultiMap class is a convenience QMap subclass that provides multi-valued maps. More...

#include <qcontainerfwd.h>

Inheritance diagram for QMultiMap< Key, T >:
QMap< Key, T >

Public Functions

QMap< Key, T >::const_iterator constFind (const Key &key, const T &value) const
 Returns an iterator pointing to the item with key key and the value value in the map. More...
 
bool contains (const Key &key, const T &value) const
 Returns true if the map contains an item with key key and value value; otherwise returns false. More...
 
int count (const Key &key, const T &value) const
 Returns the number of items with key key and value value. More...
 
QMap< Key, T >::iterator find (const Key &key, const T &value)
 Returns an iterator pointing to the item with key key and value value in the map. More...
 
QMap< Key, T >::const_iterator find (const Key &key, const T &value) const
 Returns a const iterator pointing to the item with the given key and value in the map. More...
 
QMap< Key, T >::iterator insert (const Key &key, const T &value)
 Inserts a new item with the key key and a value of value. More...
 
QMultiMap operator+ (const QMultiMap &other) const
 Returns a map that contains all the items in this map in addition to all the items in other. More...
 
QMultiMapoperator+= (const QMultiMap &other)
 Inserts all the items in the other map into this map and returns a reference to this map. More...
 
 QMultiMap ()
 Constructs an empty map. More...
 
 QMultiMap (const QMap< Key, T > &other)
 Constructs a copy of other (which can be a QMap or a QMultiMap). More...
 
int remove (const Key &key, const T &value)
 Removes all the items that have the key key and the value value from the map. More...
 
QMap< Key, T >::iterator replace (const Key &key, const T &value)
 Inserts a new item with the key key and a value of value. More...
 
void swap (QMultiMap< Key, T > &other)
 Swaps map other with this map. More...
 
- Public Functions inherited from QMap< Key, T >
iterator begin ()
 Returns an STL-style iterator pointing to the first item in the map. 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...
 
void clear ()
 Removes all items from the map. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first item in the map. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary item after the last item in the map. More...
 
const_iterator constFind (const Key &key) const
 Returns an const iterator pointing to the item with key key in the map. More...
 
bool contains (const Key &key) const
 Returns true if the map contains an item with key key; otherwise returns false. More...
 
int count (const Key &key) const
 Returns the number of items associated with key key. More...
 
int count () const
 Same as size(). More...
 
void detach ()
 Detaches this map from any other maps 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 map. 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 pointed to by the iterator pos from the map, and returns an iterator to the next item in the map. More...
 
iterator find (const Key &key)
 Returns an iterator pointing to the item with key key in the map. 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 key and a value of value. More...
 
iterator insertMulti (const Key &key, const T &value)
 Inserts a new item with the key key and a value of value. More...
 
bool isDetached () const
 Returns true if the map's internal data isn't shared with any other map object; otherwise returns false. More...
 
bool isEmpty () const
 Returns true if the map contains no items; otherwise returns false. More...
 
bool isSharedWith (const QMap< Key, T > &other) const
 
const Key key (const T &value) const
 Returns the first key with value value. More...
 
const Key key (const T &value, const Key &defaultKey) const
 Returns the first key with value value, or defaultKey if the map contains no item with value value. More...
 
QList< Key > keys () const
 Returns a list containing all the keys in the map in ascending order. More...
 
QList< Key > keys (const T &value) const
 Returns a list containing all the keys associated with value value in ascending order. More...
 
iterator lowerBound (const Key &key)
 Returns an iterator pointing to the first item with key key in the map. More...
 
const_iterator lowerBound (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...
 
bool operator!= (const QMap< Key, T > &other) const
 Returns true if other is not equal to this map; otherwise returns false. More...
 
QMap< Key, T > & operator= (const QMap< Key, T > &other)
 Assigns other to this map and returns a reference to this map. More...
 
bool operator== (const QMap< Key, T > &other) const
 Returns true if other is equal to this map; otherwise returns false. More...
 
T & operator[] (const Key &key)
 Returns the value associated with the key key as a modifiable reference. More...
 
const T operator[] (const Key &key) const
 Same as value(). More...
 
 QMap ()
 Constructs an empty map. More...
 
 QMap (const QMap< Key, T > &other)
 Constructs a copy of other. More...
 
 QMap (const typename std::map< Key, T > &other)
 
int remove (const Key &key)
 Removes all the items that have the key key from the map. More...
 
void setInsertInOrder (bool ordered)
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of (key, value) pairs in the map. More...
 
void swap (QMap< Key, T > &other)
 Swaps map other with this map. More...
 
take (const Key &key)
 Removes the item with the key key from the map and returns the value associated with it. More...
 
std::map< Key, T > toStdMap () const
 Returns an STL map equivalent to this QMap. More...
 
QList< Key > uniqueKeys () const
 Returns a list containing all the keys in the map in ascending order. More...
 
QMap< Key, T > & unite (const QMap< Key, T > &other)
 Inserts all the items in the other map into this map. More...
 
iterator upperBound (const Key &key)
 Returns an iterator pointing to the item that immediately follows the last item with key key in the map. More...
 
const_iterator upperBound (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...
 
const T value (const Key &key) const
 Returns the value associated with the key key. More...
 
const T value (const Key &key, const T &defaultValue) const
 If the map contains no item with key key, the function returns defaultValue. More...
 
QList< T > values () const
 Returns a list containing all the values in the map, in ascending order of their keys. More...
 
QList< T > values (const Key &key) const
 Returns a list containing all the values associated with key key, from the most recently inserted to the least recently inserted one. More...
 
 ~QMap ()
 Destroys the map. More...
 

Private Functions

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

Additional Inherited Members

- Public Types inherited from QMap< Key, T >
typedef const_iterator ConstIterator
 Qt-style synonym for QMap::const_iterator. More...
 
typedef qptrdiff difference_type
 Typedef for ptrdiff_t. More...
 
typedef iterator Iterator
 Qt-style synonym for QMap::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 QMultiMap< Key, T >

The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.

Note
This class or function is reentrant.

QMultiMap<Key, T> is one of Qt's generic container classes. It inherits QMap and extends it with a few convenience functions that make it more suitable than QMap for storing multi-valued maps. A multi-valued map is a map that allows multiple values with the same key; QMap normally doesn't allow that, unless you call QMap::insertMulti().

Because QMultiMap inherits QMap, all of QMap's functionality also applies to QMultiMap. For example, you can use isEmpty() to test whether the map is empty, and you can traverse a QMultiMap using QMap's iterator classes (for example, QMapIterator). But in addition, it provides an insert() function that corresponds to QMap::insertMulti(), and a replace() function that corresponds to QMap::insert(). It also provides convenient operator+() and operator+=().

Example:

QMultiMap<QString, int> map1, map2, map3;
map1.insert("plenty", 100);
map1.insert("plenty", 2000);
// map1.size() == 2
map2.insert("plenty", 5000);
// map2.size() == 1
map3 = map1 + map2;
// map3.size() == 3

Unlike QMap, QMultiMap 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 = map.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.

If you prefer the STL-style iterators, you can call find() to get the iterator for the first item with a key and iterate from there:

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

QMultiMap's key and value data types must be assignable data types. This covers most data types you are likely to encounter, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. In addition, QMultiMap's key type must provide operator<(). See the QMap documentation for details.

See also
QMap, QMapIterator, QMutableMapIterator, QMultiHash

Definition at line 59 of file qcontainerfwd.h.

Constructors and Destructors

◆ QMultiMap() [1/2]

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

Constructs an empty map.

Definition at line 976 of file qmap.h.

976 {}

◆ QMultiMap() [2/2]

template<class Key, class T>
QMultiMap< Key, T >::QMultiMap ( const QMap< Key, T > &  other)
inline

Constructs a copy of other (which can be a QMap or a QMultiMap).

See also
operator=()

Definition at line 977 of file qmap.h.

977 : QMap<Key, T>(other) {}
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

Functions

◆ constFind()

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

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

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 map contains no such item, the function returns constEnd().

See also
QMap::constFind()
QMap::constFind()

Definition at line 1040 of file qmap.h.

1041  { return find(key, value); }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with key key and value value in the map. ...
Definition: qmap.h:1020
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499

◆ contains()

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

Returns true if the map contains an item with key key and value 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
QMap::contains()
QMap::contains()

Definition at line 1048 of file qmap.h.

Referenced by QGraphicsScenePrivate::filterEvent(), and QGraphicsScenePrivate::removeSceneEventFilter().

1049 {
1050  return constFind(key, value) != QMap<Key, T>::constEnd();
1051 }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with key key and the value value in the map.
Definition: qmap.h:1040
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380

◆ count()

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

Returns the number of items with key key and value 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
QMap::count()
QMap::count()

Definition at line 1071 of file qmap.h.

Referenced by QNetworkDiskCache::expire().

1072 {
1073  int n = 0;
1076  while (i != end && !qMapLessThanKey<Key>(key, i.key())) {
1077  if (i.value() == value)
1078  ++n;
1079  ++i;
1080  }
1081  return n;
1082 }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with key key and the value value in the map.
Definition: qmap.h:1040
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ find() [1/2]

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

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

Since
4.3

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

If the map contains multiple items with key key, this function returns an iterator that points to the most recently inserted value.

See also
QMap::find()

Definition at line 1020 of file qmap.h.

1020  {
1021  typename QMap<Key, T>::iterator i(find(key));
1022  typename QMap<Key, T>::iterator end(this->end());
1023  while (i != end && !qMapLessThanKey<Key>(key, i.key())) {
1024  if (i.value() == value)
1025  return i;
1026  ++i;
1027  }
1028  return end;
1029  }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with key key and value value in the map. ...
Definition: qmap.h:1020
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
Definition: qmap.h:233
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375

◆ find() [2/2]

template<class Key, class T>
typename QMap< Key, T >::const_iterator QMultiMap< Key, T >::find ( const Key &  key,
const T &  value 
) const
inline

Returns a const iterator pointing to the item with the given key and value in the map.

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.

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

If the map contains multiple items with the specified key, this function returns a const iterator that points to the most recently inserted value.

See also
QMap::find()

Definition at line 1030 of file qmap.h.

1030  {
1033  while (i != end && !qMapLessThanKey<Key>(key, i.key())) {
1034  if (i.value() == value)
1035  return i;
1036  ++i;
1037  }
1038  return end;
1039  }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::const_iterator constFind(const Key &key, const T &value) const
Returns an iterator pointing to the item with key key and the value value in the map.
Definition: qmap.h:1040
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ insert()

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

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

If there is already an item with the same key in the map, 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 982 of file qmap.h.

Referenced by QSslCertificate::alternateSubjectNames(), QNetworkDiskCache::expire(), QGestureManager::filterEvent(), QGraphicsScenePrivate::installSceneEventFilter(), and QDBusXmlParser::interfaces().

983  { return QMap<Key, T>::insertMulti(key, value); }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
iterator insertMulti(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:595
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499

◆ operator+()

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

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

If a key is common to both maps, the resulting map will contain the key multiple times.

See also
operator+=()

Definition at line 987 of file qmap.h.

988  { QMultiMap result = *this; result += other; return result; }
The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
Definition: qcontainerfwd.h:59

◆ operator+=()

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

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

See also
insert(), operator+()

Definition at line 985 of file qmap.h.

986  { this->unite(other); return *this; }
QMap< Key, T > & unite(const QMap< Key, T > &other)
Inserts all the items in the other map into this map.
Definition: qmap.h:625

◆ operator[]() [1/2]

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

◆ operator[]() [2/2]

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

◆ remove()

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

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

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
QMap::remove()
QMap::remove()

Definition at line 1054 of file qmap.h.

1055 {
1056  int n = 0;
1057  typename QMap<Key, T>::iterator i(find(key));
1059  while (i != end && !qMapLessThanKey<Key>(key, i.key())) {
1060  if (i.value() == value) {
1061  i = this->erase(i);
1062  ++n;
1063  } else {
1064  ++i;
1065  }
1066  }
1067  return n;
1068 }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
QMap< Key, T >::iterator find(const Key &key, const T &value)
Returns an iterator pointing to the item with key key and value value in the map. ...
Definition: qmap.h:1020
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
Definition: qmap.h:233
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
iterator erase(iterator it)
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to...
Definition: qmap.h:717
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ replace()

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

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

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

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

See also
insert()

Definition at line 980 of file qmap.h.

981  { return QMap<Key, T>::insert(key, value); }
const Key key(const T &value) const
Returns the first key with value value.
Definition: qmap.h:844
const T value(const Key &key) const
Returns the value associated with the key key.
Definition: qmap.h:499
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559

◆ swap()

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

Swaps map other with this map.

Since
4.8

This operation is very fast and never fails.

Definition at line 978 of file qmap.h.

978 { QMap<Key, T>::swap(other); }
void swap(QMap< Key, T > &other)
Swaps map other with this map.
Definition: qmap.h:192

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