CopperSpice API
1.9.2
|
The QMap class is a template class which provides a sorted container of keys and values. More...
Classes | |
class | const_iterator |
STL style const iterator for QMap More... | |
class | iterator |
STL style iterator for QMap More... | |
Public Typedefs | |
using | allocator_type = typename std::map< Key, Val, C >::allocator_type |
using | const_pointer = const Val * |
using | const_reference = const Val & |
using | const_reverse_iterator = std::reverse_iterator< const_iterator > |
using | difference_type = typename std::map< Key, Val, C >::difference_type |
using | key_compare = typename std::map< Key, Val, C >::key_compare |
using | key_type = typename std::map< Key, Val, C >::key_type |
using | mapped_type = typename std::map< Key, Val, C >::mapped_type |
using | pointer = Val * |
using | reference = Val & |
using | reverse_iterator = std::reverse_iterator< iterator > |
using | size_type = typename std::map< Key, Val, C >::difference_type |
using | value_type = Val |
Public Methods | |
QMap () = default | |
QMap (C compare) | |
QMap (const QMap< Key, Val, C > &other) = default | |
QMap (const std::map< Key, Val, C > &other) | |
template<typename Input_Iterator > | |
QMap (Input_Iterator first, Input_Iterator last, const C &compare=C ()) | |
QMap (QMap< Key, Val, C > &&other) = default | |
QMap (std::initializer_list< std::pair< const Key, Val >> list, const C &compare=C ()) | |
~QMap () = default | |
iterator | begin () |
const_iterator | begin () 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 |
const_reverse_iterator | crbegin () const |
const_reverse_iterator | crend () const |
bool | empty () const |
iterator | end () |
const_iterator | end () const |
QPair< iterator, iterator > | equal_range (const Key &key) |
QPair< const_iterator, const_iterator > | equal_range (const Key &key) const |
iterator | erase (const_iterator iter) |
iterator | find (const Key &key) |
const_iterator | find (const Key &key) const |
Val & | first () |
const Val & | first () const |
const Key & | firstKey () const |
iterator | insert (const Key &key, const Val &value) |
iterator | insert (const std::pair< const Key, Val > &data) |
iterator | insert (const_iterator hint, const Key &key, const Val &value) |
bool | isEmpty () const |
const Key | key (const Val &value, const Key &defaultKey=Key ()) const |
QList< Key > | keys () const |
QList< Key > | keys (const Val &value) const |
Val & | last () |
const Val & | last () const |
const Key & | lastKey () const |
iterator | lowerBound (const Key &key) |
const_iterator | lowerBound (const Key &key) const |
bool | operator!= (const QMap< Key, Val, C > &other) const |
QMap< Key, Val, C > & | operator= (const QMap< Key, Val, C > &other) = default |
QMap< Key, Val, C > & | operator= (QMap< Key, Val, C > &&other) = default |
bool | operator== (const QMap< Key, Val, C > &other) const |
Val & | operator[] (const Key &key) |
Val | operator[] (const Key &key) const |
reverse_iterator | rbegin () |
const_reverse_iterator | rbegin () const |
size_type | remove (const Key &key) |
reverse_iterator | rend () |
const_reverse_iterator | rend () const |
size_type | size () const |
void | swap (QMap< Key, Val, C > &other) |
Val | take (const Key &key) |
std::map< Key, Val, C > | toStdMap () const |
QList< Key > | uniqueKeys () const |
QMap< Key, Val, C > & | unite (const QMap< Key, Val, C > &other) |
iterator | upperBound (const Key &key) |
const_iterator | upperBound (const Key &key) const |
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 | |
QDataStream & | operator<< (QDataStream &stream, const QMap< Key, Val, C > &map) |
QDataStream & | operator>> (QDataStream &stream, QMap< Key, Val, C > &map) |
The QMap class is a template class which provides a sorted container of keys and values. This container stores the key and value as a pair. For a given key there is one corresponding value.
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.
The following code is a simple example showing how to declare a QMap.
To insert a (key, value) pair into the map use operator[]. This example inserts the following three (key, value) pairs into the QMap: ("one", 1), ("three", 3), and ("seven", 7).
Another way to insert items into the map is with the insert() method.
The following are examples to look up a value using the operator[] or value() methods.
If you want to check whether the map contains a certain key use contains().
There is also an overload value() method which uses the second argument as a default value if there is no item with the specified key.
Items can be removed from the map in several ways. One way is to call remove() which will remove any item with the given key. The entire map can be cleared using the clear() method.
The following code looks like operator[] is doing a look up. There is a side effect of using this method. If the key is not found then an implicit insert will occur and a (key, value) pair is added. The value in the pair is value initialized.
If this is not want you intended, replace map[i]
with map.value(i)
.
To navigate through all (key, value) pairs stored in a QMap use an iterator. QMap provides both Java style iterators (QMapIterator and QMutableMapIterator) and STL style iterators (QMap::const_iterator and QMap::iterator).
The following shows how to iterate over a QMap<QString, int> using a Java style iterator.
Here is the same code using an STL style iterator.
The items are traversed in ascending key order. QMap allows only one value per key. If you call insert() with a key which already exists in the QMap, the previous value will be replaced with the new value.
To extract the values from a map and not the keys, use a "range based for loop".
The QMap<Key, Val, C> 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 QMap key type must provide operator<(). QMap uses the comparison operator to sort. Two keys x
and y
are equal if "x is not less than y" and "y is not less than x".
This is an example which shows how to declare a class which can be used as the key in a QMap. The employee class operator<() function will be used by QMap when keys are compared. If two employee;s have the same name then the dates of birth are used to break the tie.
The third template parameter in QMap<Key, Val, C> is used to define a sort order for elements in the container. This parameter is optional and is only required to override the default behavior.
The following code is the default Compare template. You do not need to do anything for this template to be called.
To change the behavior and sort your QMap differently you must create a new class, struct, or use a lambda expression as the third parameter. The following examples are declarations for QMap with a custom sort order.
The following is an example of a user defined compare class which provides "case insensitive" sorting.
QMap< Key, Val, C >::allocator_type |
Typedef for allocator used by the container.
QMap< Key, Val, C >::const_pointer |
Typedef for const Val *.
QMap< Key, Val, C >::const_reference |
Typedef for const Val &.
QMap< Key, Val, C >::const_reverse_iterator |
Typedef for an STL style const reverse iterator.
QMap< Key, Val, C >::difference_type |
Typedef for integral type used to represent the distance between two elements.
QMap< Key, Val, C >::key_compare |
Typedef for the key lessthan comparison object type.
QMap< Key, Val, C >::key_type |
Typedef for Key.
QMap< Key, Val, C >::mapped_type |
Typedef for Val.
QMap< Key, Val, C >::pointer |
Typedef for Val *.
QMap< Key, Val, C >::reference |
Typedef for Val &.
QMap< Key, Val, C >::reverse_iterator |
Typedef for an STL style reverse iterator.
QMap< Key, Val, C >::size_type |
Typedef for a signed integer of the appropriate size for your platform.
QMap< Key, Val, C >::value_type |
Typedef for Val.
|
default |
Constructs an empty QMap.
|
default |
Constructs a copy of other.
|
default |
Move constructs a map from other.
|
inline |
Constructs a map with a copy of each of the elements in the specified initializer list.
If the function object compare is passed it will be called when sorting keys in the container. Refer to Overriding Sort Order for more information about this template parameter.
|
inlineexplicit |
Constructs an empty map using the specified function object compare. Refer to Overriding Sort Order for more information about this parameter.
|
inlineexplicit |
Constructs a copy of other.
|
inline |
Constructs a map containing copies of the elements between first and last.
If the function object compare is passed it will be called when sorting keys in the container. Refer to Overriding Sort Order for more information about this template parameter.
|
default |
Destroys the map. References to the values in the map and all iterators over this map become invalid.
|
inline |
Returns an STL style iterator pointing to the first item in the map.
|
inline |
Returns a const STL style iterator pointing to the first item in the map.
|
inline |
|
inline |
|
inline |
Removes all items from the map.
|
inline |
Returns a const STL style iterator pointing to the first item in the map.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the map.
|
inline |
Returns a const iterator pointing to the item with the given key. If the map contains no item with key, the method returns constEnd().
|
inline |
Returns true if the map contains an item the given key, otherwise it returns false.
|
inline |
Equivalent to calling size().
|
inline |
Returns the number of items associated with the specified key.
|
inline |
|
inline |
|
inline |
This method is equivalent to isEmpty(), returning true if the map is empty, otherwise returning false.
|
inline |
Returns an STL style iterator pointing to the imaginary item after the last item in the map.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the map.
|
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().
|
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().
|
inline |
Removes the (key, value) pair pointed to by the iterator iter from the map and returns an iterator to the next item in the map.
This method assumes the iterator is valid and refers to an element in the map. It will not check if iter points past the end of the map or is equal to the end() iterator.
|
inline |
Returns an iterator pointing to the item with the specified key. If the map contains no item with this key, the method returns end().
|
inline |
This is an overloaded method.
|
inline |
Returns a reference to the first value in the map. This method assumes the map is not empty.
|
inline |
Returns a const reference to the first value in the map. This method assumes the map is not empty.
|
inline |
Returns a const reference to the first key in the map. This method assumes the map is not empty.
|
inline |
Inserts a new item with the key and value. If there is already an item with this key, the value for the item is replaced with value. This is the same behavior as insert_or_assign() in std::map.
|
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.
|
inline |
Inserts a new item with the key and value using hint as a suggestion about where to insert. If there is already an item with the specified key, the item's value is replaced with value.
|
inline |
Returns true if the map contains no items, otherwise returns false.
const Key QMap< Key, Val, C >::key | ( | const Val & | value, |
const Key & | defaultKey = Key() |
||
) | const |
Returns the first key with value. If the map contains no item with the given value the defaultKey is returned. If no defaultKey was passed it will be value initialized using the data type of Key. 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.
QList< Key > QMap< Key, Val, C >::keys | ( | ) | const |
Returns a list containing all the keys in the map in ascending order. 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 returned by values().
QList< Key > QMap< Key, Val, C >::keys | ( | const Val & | value | ) | const |
Returns a list containing all the keys associated with specified value in ascending order.
This method can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.
|
inline |
Returns a reference to the last value in the map. This method assumes the map is not empty.
|
inline |
Returns a const reference to the last value in the map. This method assumes the map is not empty.
|
inline |
Returns a const reference to the last key in the map. This method assumes the map is not empty.
|
inline |
Returns an iterator pointing to the first item with the given key. If the map contains no item with key, the method returns an iterator to the nearest item with a greater key.
|
inline |
This is an overloaded method.
|
inline |
Returns true if other is not equal to this map, otherwise returns false. Two maps are considered equal if they contain the same (key, value) pairs.
This method requires the type T to support operator==()
.
|
default |
Copy assigns from other and returns a reference to this object.
|
default |
Move assigns from other and returns a reference to this object.
|
inline |
Returns true if other is equal to this map, otherwise returns false. Two maps are considered equal if they contain the same (key, value) pairs.
This method requires the type T to support operator==()
.
Val & QMap< Key, Val, C >::operator[] | ( | const Key & | key | ) |
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.
|
inline |
|
inline |
This is an overloaded method.
|
inline |
|
inline |
|
inline |
|
inline |
This is an overloaded method.
|
inline |
|
inline |
Swaps other with this map. This operation is very fast and never fails.
|
inline |
Removes the item with the given key and returns the value. The element is then erased. If you are not using the return value calling remove() will be more efficient. If the key was not found then a value initialized object of type Val will be returned. Refer to default constructed elements for additional information.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value and then erase the element. Call operator[] or find() and then access the data by calling iter.value(). Call erase() to remove the element.
std::map< Key, Val, C > QMap< Key, Val, C >::toStdMap | ( | ) | const |
Returns an STL map equivalent to this QMap.
QList< Key > QMap< Key, Val, C >::uniqueKeys | ( | ) | const |
Returns a list of every key in the map with out duplicates.
|
inline |
Inserts all the items in the other map into this map. If a key is common to both maps the resulting map will contain the old value.
|
inline |
Returns an iterator pointing to the item which immediately follows the last item with specified key. If the map contains no item with specified key, the method returns an iterator to the nearest item with a greater key.
|
inline |
This is an overloaded method.
Val QMap< Key, Val, C >::value | ( | const Key & | key | ) | const |
Returns the value associated with the specified key. If no item with the given key is found, a new value initialized object of type Val with the given key is returned. If no defaultValue was passed it will be value initialized using the data type of Val. Refer to default constructed elements for additional information.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value. Call operator[] or find() and then access the data by calling iter.value().
Val QMap< Key, Val, C >::value | ( | const Key & | key, |
const Val & | defaultValue | ||
) | const |
Returns the value associated with the specified key. If the map contains no element with the given key then defaultValue is returned. If no defaultValue was passed it will be value initialized using the data type of Val. Refer to default constructed elements for additional information.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value. Call find() and check if the key was found.
QList< Val > QMap< Key, Val, C >::values | ( | ) | const |
|
related |
Writes the given map to the stream. Returns a reference to the stream. This function requires the types Key and Val to support operator<<()
.
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given map. Returns a reference to the stream. This function requires the types Key and Val to support operator>>()
.
Refer to Serializing Data Types for additional information.