summaryrefslogtreecommitdiffstats
path: root/doc/man/man3/tqmap.3qt
blob: 169d558d69d7570156adfc630ea40ba54dbb8f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
'\" t
.TH TQMap 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA.  All rights reserved.  See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
TQMap \- Value-based template class that provides a dictionary
.SH SYNOPSIS
\fC#include <tqmap.h>\fR
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "typedef Key \fBkey_type\fR"
.br
.ti -1c
.BI "typedef T \fBmapped_type\fR"
.br
.ti -1c
.BI "typedef TQPair<const key_type, mapped_type> \fBvalue_type\fR"
.br
.ti -1c
.BI "typedef value_type * \fBpointer\fR"
.br
.ti -1c
.BI "typedef const value_type * \fBconst_pointer\fR"
.br
.ti -1c
.BI "typedef value_type & \fBreference\fR"
.br
.ti -1c
.BI "typedef const value_type & \fBconst_reference\fR"
.br
.ti -1c
.BI "typedef size_t \fBsize_type\fR"
.br
.ti -1c
.BI "typedef TQMapIterator<Key, T> \fBiterator\fR"
.br
.ti -1c
.BI "typedef TQMapConstIterator<Key, T> \fBconst_iterator\fR"
.br
.ti -1c
.BI "typedef TQPair<iterator, bool> \fBinsert_pair\fR"
.br
.ti -1c
.BI "typedef TQMapIterator<Key, T> \fBIterator\fR"
.br
.ti -1c
.BI "typedef TQMapConstIterator<Key, T> \fBConstIterator\fR"
.br
.ti -1c
.BI "typedef T \fBValueType\fR"
.br
.ti -1c
.BI "\fBTQMap\fR ()"
.br
.ti -1c
.BI "\fBTQMap\fR ( const TQMap<Key, T> & m )"
.br
.ti -1c
.BI "\fBTQMap\fR ( const std::map<Key, T> & m )"
.br
.ti -1c
.BI "\fB~TQMap\fR ()"
.br
.ti -1c
.BI "TQMap<Key, T> & \fBoperator=\fR ( const TQMap<Key, T> & m )"
.br
.ti -1c
.BI "TQMap<Key, T> & \fBoperator=\fR ( const std::map<Key, T> & m )"
.br
.ti -1c
.BI "iterator \fBbegin\fR ()"
.br
.ti -1c
.BI "iterator \fBend\fR ()"
.br
.ti -1c
.BI "const_iterator \fBbegin\fR () const"
.br
.ti -1c
.BI "const_iterator \fBend\fR () const"
.br
.ti -1c
.BI "const_iterator \fBconstBegin\fR () const"
.br
.ti -1c
.BI "const_iterator \fBconstEnd\fR () const"
.br
.ti -1c
.BI "iterator \fBreplace\fR ( const Key & k, const T & v )"
.br
.ti -1c
.BI "size_type \fBsize\fR () const"
.br
.ti -1c
.BI "bool \fBempty\fR () const"
.br
.ti -1c
.BI "TQPair<iterator, bool> \fBinsert\fR ( const value_type & x )"
.br
.ti -1c
.BI "void \fBerase\fR ( iterator it )"
.br
.ti -1c
.BI "void \fBerase\fR ( const key_type & k )"
.br
.ti -1c
.BI "size_type \fBcount\fR ( const key_type & k ) const"
.br
.ti -1c
.BI "T & \fBoperator[]\fR ( const Key & k )"
.br
.ti -1c
.BI "void \fBclear\fR ()"
.br
.ti -1c
.BI "iterator \fBfind\fR ( const Key & k )"
.br
.ti -1c
.BI "const_iterator \fBfind\fR ( const Key & k ) const"
.br
.ti -1c
.BI "const T & \fBoperator[]\fR ( const Key & k ) const"
.br
.ti -1c
.BI "bool \fBcontains\fR ( const Key & k ) const"
.br
.ti -1c
.BI "size_type \fBcount\fR () const"
.br
.ti -1c
.BI "TQValueList<Key> \fBkeys\fR () const"
.br
.ti -1c
.BI "TQValueList<T> \fBvalues\fR () const"
.br
.ti -1c
.BI "bool \fBisEmpty\fR () const"
.br
.ti -1c
.BI "iterator \fBinsert\fR ( const Key & key, const T & value, bool overwrite = TRUE )"
.br
.ti -1c
.BI "void \fBremove\fR ( iterator it )"
.br
.ti -1c
.BI "void \fBremove\fR ( const Key & k )"
.br
.in -1c
.SS "Protected Members"
.in +1c
.ti -1c
.BI "void \fBdetach\fR ()"
.br
.in -1c
.SH RELATED FUNCTION DOCUMENTATION
.in +1c
.ti -1c
.BI "QDataStream & \fBoperator>>\fR ( QDataStream & s, TQMap<Key, T> & m )"
.br
.ti -1c
.BI "QDataStream & \fBoperator<<\fR ( QDataStream & s, const TQMap<Key, T> & m )"
.br
.in -1c
.SH DESCRIPTION
The TQMap class is a value-based template class that provides a dictionary.
.PP
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.
.PP
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 TQPtrList and QDict are pointer-based.
.PP
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.
.PP
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
.TP
A copy constructor
.TP
An assignment operator
.TP
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.
.PP
The class used for the key requires that the \fCoperator<\fR is implemented to define ordering of the keys.
.PP
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.
.PP
Example:
.PP
.nf
.br
    #include <tqstring.h>
.br
    #include <tqmap.h>
.br
    #include <tqstring.h>
.br
.br
    class Employee
.br
    {
.br
    public:
.br
        Employee(): sn(0) {}
.br
        Employee( const TQString& forename, const TQString& surname, int salary )
.br
            : fn(forename), sn(surname), sal(salary)
.br
        { }
.br
.br
        TQString forename() const { return fn; }
.br
        TQString surname() const { return sn; }
.br
        int salary() const { return sal; }
.br
        void setSalary( int salary ) { sal = salary; }
.br
.br
    private:
.br
        TQString fn;
.br
        TQString sn;
.br
        int sal;
.br
    };
.br
.br
    int main(int argc, char **argv)
.br
    {
.br
        QApplication app( argc, argv );
.br
.br
        typedef TQMap<TQString, Employee> EmployeeMap;
.br
        EmployeeMap map;
.br
.br
        map["JD001"] = Employee("John", "Doe", 50000);
.br
        map["JW002"] = Employee("Jane", "Williams", 80000);
.br
        map["TJ001"] = Employee("Tom", "Jones", 60000);
.br
.br
        Employee sasha( "Sasha", "Hind", 50000 );
.br
        map["SH001"] = sasha;
.br
        sasha.setSalary( 40000 );
.br
.br
        EmployeeMap::Iterator it;
.br
        for ( it = map.begin(); it != map.end(); ++it ) {
.br
            printf( "%s: %s, %s earns %d\\n",
.br
                    it.key().latin1(),
.br
                    it.data().surname().latin1(),
.br
                    it.data().forename().latin1(),
.br
                    it.data().salary() );
.br
        }
.br
        return 0;
.br
    }
.br
.fi
.PP
Program output:
.PP
.nf
.br
    JD001: Doe, John earns 50000
.br
    JW002: Williams, Jane earns 80000
.br
    SH001: Hind, Sasha earns 50000
.br
    TJ001: Jones, Tom earns 60000
.br
.fi
.PP
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:
.PP
.nf
.br
        TQMap<TQString,TQString> map;
.br
        map["Clinton"] = "Bill";
.br
        str << map["Clinton"] << map["Bush"] << endl;
.br
.fi
.PP
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.
.PP
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.
.PP
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 TQMutex.
.PP
There are a couple of ways of inserting new items into the map. One uses the insert() method; the other uses operator[]:
.PP
.nf
.br
    TQMap<TQString, TQString> map;
.br
    map["Clinton"] = "Bill";
.br
    map.insert( "Bush", "George" );
.br
.fi
.PP
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.
.PP
See also TQMapIterator, TQt Template Library Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes.
.SS "Member Type Documentation"
.SH "TQMap::ConstIterator"
The map's const iterator type, TQt style.
.SH "TQMap::Iterator"
The map's iterator type, TQt style.
.SH "TQMap::ValueType"
Corresponds to TQPair<key_type, mapped_type>, TQt style.
.SH "TQMap::const_iterator"
The map's const iterator type.
.SH "TQMap::const_pointer"
Const pointer to value_type.
.SH "TQMap::const_reference"
Const reference to value_type.
.SH "TQMap::iterator"
The map's iterator type.
.SH "TQMap::key_type"
The map's key type.
.SH "TQMap::mapped_type"
The map's data type.
.SH "TQMap::pointer"
Pointer to value_type.
.SH "TQMap::reference"
Reference to value_type.
.SH "TQMap::size_type"
An unsigned integral type, used to represent various sizes.
.SH "TQMap::value_type"
Corresponds to TQPair<key_type, mapped_type>.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "TQMap::TQMap ()"
Constructs an empty map.
.SH "TQMap::TQMap ( const TQMap<Key, T> & m )"
Constructs a copy of \fIm\fR.
.PP
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.
.SH "TQMap::TQMap ( const std::map<Key, T> & m )"
Constructs a copy of \fIm\fR.
.SH "TQMap::~TQMap ()"
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.
.SH "iterator TQMap::begin ()"
Returns an iterator pointing to the first element in the map. This iterator equals end() if the map is empty.
.PP
The items in the map are traversed in the order defined by operator<(Key, Key).
.PP
See also end() and TQMapIterator.
.SH "const_iterator TQMap::begin () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
See also end() and TQMapConstIterator.
.SH "void TQMap::clear ()"
Removes all items from the map.
.PP
See also remove().
.SH "const_iterator TQMap::constBegin () const"
Returns an iterator pointing to the first element in the map. This iterator equals end() if the map is empty.
.PP
The items in the map are traversed in the order defined by operator<(Key, Key).
.PP
See also constEnd() and TQMapConstIterator.
.SH "const_iterator TQMap::constEnd () const"
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.
.PP
See also constBegin() and TQMapConstIterator.
.SH "bool TQMap::contains ( const Key & k ) const"
Returns TRUE if the map contains an item with key \fIk\fR; otherwise returns FALSE.
.SH "size_type TQMap::count ( const key_type & k ) const"
Returns the number of items whose key is \fIk\fR. Since TQMap does not allow duplicate keys, the return value is always 0 or 1.
.PP
This function is provided for STL compatibility.
.SH "size_type TQMap::count () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns the number of items in the map.
.PP
See also isEmpty().
.SH "void TQMap::detach ()\fC [protected]\fR"
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.
.SH "bool TQMap::empty () const"
Returns TRUE if the map contains no items; otherwise returns FALSE.
.PP
This function is provided for STL compatibility. It is equivalent to isEmpty().
.PP
See also size().
.SH "iterator TQMap::end ()"
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 begin() if the map is empty.
.PP
See also begin() and TQMapIterator.
.SH "const_iterator TQMap::end () const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.SH "void TQMap::erase ( iterator it )"
Removes the item associated with the iterator \fIit\fR from the map.
.PP
This function is provided for STL compatibility. It is equivalent to remove().
.PP
See also clear().
.SH "void TQMap::erase ( const key_type & k )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Removes the item with the key \fIk\fR from the map.
.SH "iterator TQMap::find ( const Key & k )"
Returns an iterator pointing to the element with key \fIk\fR in the map.
.PP
Returns end() if no key matched.
.PP
See also TQMapIterator.
.SH "const_iterator TQMap::find ( const Key & k ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns an iterator pointing to the element with key \fIk\fR in the map.
.PP
Returns end() if no key matched.
.PP
See also TQMapConstIterator.
.SH "iterator TQMap::insert ( const Key & key, const T & value, bool overwrite = TRUE )"
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.
.SH "TQPair<iterator, bool> TQMap::insert ( const value_type & x )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
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.
.PP
See also replace().
.SH "bool TQMap::isEmpty () const"
Returns TRUE if the map contains no items; otherwise returns FALSE.
.PP
See also count().
.SH "TQValueList<Key> TQMap::keys () const"
Returns a list of all the keys in the map, in order.
.SH "TQMap<Key, T> & TQMap::operator= ( const TQMap<Key, T> & m )"
Assigns \fIm\fR to this map and returns a reference to this map.
.PP
All iterators of the current map become invalidated by this operation. The cost of such an assignment is O(1), because TQMap is implicitly shared.
.SH "TQMap<Key, T> & TQMap::operator= ( const std::map<Key, T> & m )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Assigns \fIm\fR to this map and returns a reference to this map.
.PP
All iterators of the current map become invalidated by this operation.
.SH "T & TQMap::operator[] ( const Key & k )"
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:
.PP
.nf
.br
    TQMap<TQString, TQString> map;
.br
    map["Clinton"] = "Bill";
.br
    stream << map["Clinton"];
.br
.fi
.SH "const T & TQMap::operator[] ( const Key & k ) const"
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.
.SH "void TQMap::remove ( iterator it )"
Removes the item associated with the iterator \fIit\fR from the map.
.PP
See also clear().
.SH "void TQMap::remove ( const Key & k )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Removes the item with the key \fIk\fR from the map.
.SH "iterator TQMap::replace ( const Key & k, const T & v )"
Replaces the value of the element with key \fIk\fR, with the value \fIv\fR.
.PP
See also insert() and remove().
.SH "size_type TQMap::size () const"
Returns the number of items in the map.
.PP
This function is provided for STL compatibility. It is equivalent to count().
.PP
See also empty().
.SH "TQValueList<T> TQMap::values () const"
Returns a list of all the values in the map, in key order.
.SH RELATED FUNCTION DOCUMENTATION
.SH "QDataStream & operator<< ( QDataStream & s, const TQMap<Key, T> & m )"
Writes the map \fIm\fR to the stream \fIs\fR. The types \fIKey\fR and \fIT\fR must implement the streaming operator as well.
.SH "QDataStream & operator>> ( QDataStream & s, TQMap<Key, T> & m )"
Reads the map \fIm\fR from the stream \fIs\fR. The types \fIKey\fR and \fIT\fR
must implement the streaming operator as well.

.SH "SEE ALSO"
.BR http://doc.trolltech.com/tqmap.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com.  See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $TQTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech. 
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqmap.3qt) and the Qt
version (3.3.8).