Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QMultiMap< Key, T > Class Template Reference

Inheritance diagram for QMultiMap< Key, T >:

QMap< Key, T >

List of all members.


Detailed Description

template<class Key, class T>
class QMultiMap< Key, T >

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

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:

        QMultiMap<QString, int>::iterator i = map.find("plenty");
        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 905 of file qmap.h.


Public Types

typedef const_iterator ConstIterator
typedef ptrdiff_t difference_type
typedef iterator Iterator
typedef Key key_type
typedef T mapped_type
typedef int size_type

Public Member Functions

const_iterator begin () const
iterator begin ()
void clear ()
const_iterator constBegin () const
const_iterator constEnd () const
const_iterator constFind (const Key &key) const
QMap< Key, T >::const_iterator constFind (const Key &key, const T &value) const
bool contains (const Key &key) const
bool contains (const Key &key, const T &value) const
int count () const
int count (const Key &key) const
int count (const Key &key, const T &value) const
void detach ()
bool empty () const
const_iterator end () const
iterator end ()
iterator erase (iterator it)
const_iterator find (const Key &key) const
iterator find (const Key &key)
QMap< Key, T >::const_iterator find (const Key &key, const T &value) const
QMap< Key, T >::iterator find (const Key &key, const T &value)
QMap< Key, T >::iterator insert (const Key &key, const T &value)
iterator insertMulti (const Key &key, const T &value)
bool isDetached () const
bool isEmpty () const
const Key key (const T &value, const Key &defaultKey) const
const Key key (const T &value) const
QList< Key > keys (const T &value) const
QList< Key > keys () const
const_iterator lowerBound (const Key &key) const
iterator lowerBound (const Key &key)
bool operator!= (const QMap< Key, T > &other) const
QMultiMap operator+ (const QMultiMap &other) const
QMultiMapoperator+= (const QMultiMap &other)
bool operator== (const QMap< Key, T > &other) const
 QMultiMap (const QMap< Key, T > &other)
 QMultiMap ()
int remove (const Key &key)
int remove (const Key &key, const T &value)
QMap< Key, T >::iterator replace (const Key &key, const T &value)
void setInsertInOrder (bool ordered)
void setSharable (bool sharable)
int size () const
take (const Key &key)
std::map< Key, T > toStdMap () const
QList< Key > uniqueKeys () const
QMap< Key, T > & unite (const QMap< Key, T > &other)
const_iterator upperBound (const Key &key) const
iterator upperBound (const Key &key)
const T value (const Key &key, const T &defaultValue) const
const T value (const Key &key) const
QList< T > values (const Key &key) const
QList< T > values () const

Private Member Functions

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

Friends

class const_iterator
class iterator

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QMap< Key, T > &map)
QDataStreamoperator>> (QDataStream &in, QMap< Key, T > &map)

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

Generated by  Doxygen 1.6.0   Back to index