TQMap is a TQt implementation of an STL-like map container. It can be used in your application if the standard \fCmap\fR is not available on all your target platforms. TQMap is part of the TQt Template Library.
TQMap<Key, Data> defines a template instance to create a dictionary with keys of type Key and values of type Data. TQMap does not store pointers to the members of the map; instead, it holds a copy of every member. For this reason, TQMap is value-based, whereas QPtrList and QDict are pointer-based.
TQMap contains and manages a collection of objects of type Data with associated key values of type Key and provides iterators that allow the contained objects to be addressed. TQMap owns the contained items.
Some classes cannot be used within a TQMap. For example everything derived from TQObject and thus all classes that implement widgets. Only values can be used in a TQMap. To qualify as a value, the class must provide
A default constructor, i.e. a constructor that does not take any arguments.
.PP
Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases, this is sufficient.
TQMap's function naming is consistent with the other TQt classes (e.g., count(), isEmpty()). TQMap also provides extra functions for compatibility with STL algorithms, such as size() and empty(). Programmers already familiar with the STL \fCmap\fR can use these the STL-like functions if preferred.
The latest changes to Sasha's salary did not affect the value in the list because the map created a copy of Sasha's entry. In addition, notice that the items are sorted alphabetically (by key) when iterating over the map.
.PP
There are several ways to find items in a map. The begin() and end() functions return iterators to the beginning and end of the map. The advantage of using an iterator is that you can move forward or backward by incrementing/decrementing the iterator. The iterator returned by end() points to the element which is one past the last element in the container. The past-the-end iterator is still associated with the map it belongs to, however it is \fInot\fR dereferenceable; operator*() will not return a well-defined value. If the map is empty, the iterator returned by begin() will equal the iterator returned by end().
.PP
Another way to find an element in the map is by using the find() function. This returns an iterator pointing to the desired item or to the end() iterator if no such element exists.
.PP
Another approach uses the operator[]. But be warned: if the map does not contain an entry for the element you are looking for, operator[] inserts a default value. If you do not know that the element you are searching for is really in the list, you should not use operator[]. The following example illustrates this:
The code fragment will print out "Clinton", "". Since the value associated with the "Bush" key did not exist, the map inserted a default value (in this case, an empty string). If you are not sure whether a certain element is in the map, you should use find() and iterators instead.
.PP
If you just want to know whether a certain key is contained in the map, use the contains() function. In addition, count() tells you how many keys are in the map.
.PP
It is safe to have multiple iterators at the same time. If some member of the map is removed, only iterators pointing to the removed member become invalid; inserting in the map does not invalidate any iterators.
Since TQMap is value-based, there is no need to be concerned about deleting items in the map. The map holds its own copies and will free them if the corresponding member or the map itself is deleted.
TQMap is implicitly shared. This means you can just make copies of the map in time O(1). If multiple TQMap instances share the same data and one is modifying the map's data, this modifying instance makes a copy and modifies its private copy: so it does not affect other instances. If a TQMap is being used in a multi-threaded program, you must protect all access to the map. See QMutex.
Items can also be removed from the map in several ways. One way is to pass an iterator to remove(). Another way is to pass a key value to remove(), which will delete the entry with the requested key. In addition you can clear the entire map using the clear() method.
This operation costs O(1) time because TQMap is implicitly shared. This makes returning a TQMap from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes O(n) time.
Destroys the map. References to the values in the map and all iterators of this map become invalidated. Since TQMap is highly tuned for performance you won't see warnings if you use invalid iterators, because it is not possible for an iterator to check whether it is valid or not.
The iterator returned by end() points to the element which is one past the last element in the container. The past-the-end iterator is still associated with the map it belongs to, but it is \fInot\fR dereferenceable; operator*() will not return a well-defined value.
.PP
This iterator equals constBegin() if the map is empty.
If the map does not share its data with another TQMap instance, nothing happens; otherwise the function creates a new copy of this map and detaches from the shared one. This function is called whenever the map is modified. The implicit sharing mechanism is implemented this way.
The iterator returned by end() points to the element which is one past the last element in the container. The past-the-end iterator is still associated with the map it belongs to, but it is \fInot\fR dereferenceable; operator*() will not return a well-defined value.
Inserts a new item with the key, \fIkey\fR, and a value of \fIvalue\fR. If there is already an item whose key is \fIkey\fR, that item's value is replaced with \fIvalue\fR, unless \fIoverwrite\fR is FALSE (it is TRUE by default). In this case an iterator to this item is returned, else an iterator to the new item is returned.
Inserts the (key, value) pair \fIx\fR into the map. \fIx\fR is a TQPair whose \fCfirst\fR element is a key to be inserted and whose \fCsecond\fR element is the associated value to be inserted. Returns a pair whose \fCfirst\fR element is an iterator pointing to the inserted item and whose \fCsecond\fR element is a bool indicating TRUE if \fIx\fR was inserted and FALSE if it was not inserted, e.g. because it was already present.
Returns the value associated with the key \fIk\fR. If no such key is present, an empty item is inserted with this key and a reference to the empty item is returned.
.PP
You can use this operator both for reading and writing:
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
\fBWarning:\fR This function differs from the non-const version of the same function. It will \fInot\fR insert an empty value if the key \fIk\fR does not exist. This may lead to logic errors in your program. You should check if the element exists before calling this function.
.PP
Returns the value associated with the key \fIk\fR. If no such key is present, a reference to an empty item is returned.