📄 qmap.3qt
字号:
'\" t.TH QMap 3qt "17 July 2003" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2001 Trolltech AS. All rights reserved. See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQMap \- Value based template class that provides a dictionary.br.PP\fC#include <qmap.h>\fR.PP.SS "Public Members".in +1c.ti -1c.BI "\fBQMap\fR () ".br.ti -1c.BI "\fBQMap\fR ( const QMap<Key,T> & m ) ".br.ti -1c.BI "\fB~QMap\fR () ".br.ti -1c.BI "QMap<Key, T>& \fBoperator=\fR ( const QMap<Key, T> & m ) ".br.ti -1c.BI "Iterator \fBbegin\fR () ".br.ti -1c.BI "Iterator \fBend\fR () ".br.ti -1c.BI "ConstIterator \fBbegin\fR () const".br.ti -1c.BI "ConstIterator \fBend\fR () const".br.ti -1c.BI "Iterator \fBfind\fR ( const Key & k ) ".br.ti -1c.BI "ConstIterator \fBfind\fR ( const Key & k ) const".br.ti -1c.BI "T& \fBoperator[]\fR ( const Key & k ) ".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 "uint \fBcount\fR () const".br.ti -1c.BI "bool \fBisEmpty\fR () const".br.ti -1c.BI "Iterator \fBinsert\fR ( const Key & " "key" ", const T & value ) ".br.ti -1c.BI "void \fBremove\fR ( Iterator it ) ".br.ti -1c.BI "void \fBremove\fR ( const Key & k ) ".br.ti -1c.BI "Iterator \fBreplace\fR ( const Key & " "k" ", const T & v ) ".br.ti -1c.BI "void \fBclear\fR () ".br.in -1c.SS "Protected Members".in +1c.ti -1c.BI "void \fBdetach\fR () ".br.in -1c.SH RELATED FUNCTION DOCUMENTATION(Note that these are not member functions.).in +1c.ti -1c.BI "QDataStream& \fBoperator>>\fR (QDataStream & " "s" ", QMap<Key,T> & " "m" ")".br.ti -1c.BI "QDataStream& \fBoperator<<\fR (QDataStream & " "s" ", const QMap<Key,T> & " "m" ")".br.in -1c.SH DESCRIPTIONThe QMap class is a value based template class that provides a dictionary.PPDefine a template instance QMap<Key,Data> to create a dictionary with keys of type Key and values of type Data. QMap does not store pointers to the members of the map. Instead, it holds a copy of every member. For that reason this kind of classes is called "value based" while QList and QDict are "reference based"..PPSome classes can not be used within a QMap, for example everything derived from QObject and thus all classes that implement widgets. Only values can be used in a QMap. To qualify as a value, the class must provide.TPa copy constructor,.TPan assignment operator and.TPa default constructor, i.e. a constructor that does not take any arguments..PPNote that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases, this is sufficient..PPThe class used for the key requires that the \fCoperator<\fR is implemented and defines a total order on the keys..PPExample:.PP.nf.br #include <qmap.h>.br #include <qstring.h>.br #include <stdio.h>.br.br class Employee.br {.br public:.br Employee(): s(0) {}.br Employee( const QString& name, int salary ).br : n(name), s(salary).br {}.br.br QString name() const { return n; }.br int salary() const { return s; }.br void setSalary( int salary ) { s = salary; }.br private:.br QString n;.br int s;.br };.br.br void main().br {.br typedef QMap<QString,Employee> EmployeeMap;.br EmployeeMap map; // map of Employee.br.br map.insert( "Gates", Employee("Bill", 50000) );.br map.insert( "Ballmer", Employee("Steve",80000) );.br map.insert( "Sommer,", Employee("Ron", 60000) );.br.br Employee joe( "Joe", 50000 );.br map.insert( "Doe", joe );.br joe.setSalary( 4000 );.br.br EmployeeMap::Iterator it;.br for( it = map.begin(); it != map.end(); ++it ).br printf( "%s, %s earns %d\\n", it.key().latin1(), it.data().name().latin1(), it.data().salary() );.br }.fi.PPProgram output:.PP.nf.br Ballmer, Steve earns 80000.br Doe, Joe earns 50000.br Gates, Bill earns 50000.br Sommer, Ron earns 60000.fi.PPAs you can see, the latest changes to Joe's salary did not affect the value in the list because the map created a copy of Joe's entry. In addition you should notice that the items are alphabetically sorted when iterating over the map..PPThere are two ways to find values in the list. The first one is to use the find() function. It returns an iterator pointing to the desired item or the end() iterator it no such element exists..PPThe second approach uses the operator[]. But be warned: If you don't know that the element you are searching for is really in the list, then you should not use operator[]. The following example illustrates that..PP.nf.br QMap<QString,QString> map;.br map.insert( "Weis", "Torben" );.br str << map["Weis"] << map["Ettrich"] << endl;.br.br const QMap<QString,QString>& map2 = map;.br str << map2["Weis"] << map2["Reggie"] << endl;.fi.PPThe code fragment will print out "Torben", "" and the second part will print "Torben", "". In addition the first fragment inserted an empty entry with key "Ettrich". The second one did not insert an empty entry with key "Reggie" because the const operator[] was used which can not do insertion. So if you are not sure whether a certain element is in the map you should use find() and iterators..PPIf you just want to know whether a certain key is contained in the map, the the contains() function is what you are looking for. In addition count() tells you how many keys there are currently in the map..PPAnother method for traversing a map is to use the functions begin() and end(). With a simple for loop as shown in the example you can iterate over the complete map. It is safe to have multiple iterators at the same time. If some member of the map is removed then only iterators pointing to the removed member become invalid. Inserting in the map does not invalidate any iterator..PPSince QMap is value based there is no need to care about deleting elements in the list. The list holds its own copies and will free them if the corresponding member or the list itself is deleted. You can force the list to free all of its item with clear()..PPQMap is implicitly shared. This means you can just make copies of the map in time O(1). If multiple QMap instances share the same data and one is modifying the map's data then this modifying instance makes a copy and modifies its private copy - thus it does not affect other instances. From a developer's point of view you can think that a QMap and a copy of this map have nothing to do with each other..PPThere are two ways of inserting new elements in a list. One uses the insert() method while the other one uses operator[] like this:.PP.nf.br QMap<QString,QString> map;.br map["Weis"] = "Torben";.fi;.PPItems can be removed from the map in two ways. The first is to pass an iterator to the remove(). The other possibility 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..PPSee also QMapIterator..SH MEMBER FUNCTION DOCUMENTATION.SH "QMap::QMap ()"Constructs an empty map..SH "QMap::QMap ( const QMap<Key,T> & m )"Constructs a copy of \fIm.\fR.PPThis operation costs O(1) time since QMap is implicit shared. The first instance applying modifications to a shared list will create a copy which takes in turn O(n) time. However returning a QMap from a function is very fast..SH "QMap::~QMap ()"Destroys the map. References to the values in the map and all iterators of this map become invalidated. Since QMap is highly tuned for performance you won't see warnings if you use invalid iterators, because it is impossible for an iterator to check whether it is valid or not..SH "ConstIterator QMap::begin () const"Returns an iterator pointing to the first element in the map. This iterator equals end() if the map is empty;.PPSee also end() and QMapConstIterator..SH "Iterator QMap::begin ()"Returns an iterator pointing to the first element in the map. This iterator equals end() if the map is empty;.PPSee also end() and QMapIterator..SH "void QMap::clear ()"Removes all items from the map..PPSee also remove()..SH "bool QMap::contains ( const Key & k ) const"Returns TRUE if the key \fIk\fR is contained in the map..SH "uint QMap::count () const"Returns the number of items in the ap..PPSee also isEmpty()..SH "void QMap::detach () \fC[protected]\fR"If the map does not share its data with another QMap instance, then nothing happens, otherwise the function creates a new copy of this data and detaches from the shared one. This function is called whenever the map is modified. The implicit sharing mechanism is implemented this way..SH "ConstIterator QMap::end () const"Returns an iterator pointing behind the last element in the map. This iterator equals begin() if the map is empty..PPSee also begin() and QMapConstIterator..SH "Iterator QMap::end ()"Returns an iterator pointing behind the last element in the map. This iterator equals begin() if the map is empty..PPSee also begin() and QMapIterator..SH "ConstIterator QMap::find ( const Key & k ) const"Finds the key \fIk\fR in the map..PPReturns end() if no key did match..PPSee also QMapConstIterator..SH "Iterator QMap::find ( const Key & k )"Finds the key \fIk\fR in the map..PPReturns end() if no key did match..PPSee also QMapIterator..SH "Iterator QMap::insert ( const Key & key, const T & value )"Inserts the \fIvalue\fR with key \fIk.\fR.PPReturns an iterator pointing at the inserted value..PPSee also QMapIterator..SH "bool QMap::isEmpty () const"Returns TRUE if the list is empty, i.e. count() == 0. Returns FALSE otherwise..PPSee also count()..SH "QMap<Key, T>& QMap::operator= ( const QMap<Key, T> & m )"Assigns \fIm\fR to this map and returns a reference to this map..PPAll iterators of the current map become invalidated by this operation. The cost of such an assignment is O(1) since QMap is implicitly shared..SH "T& QMap::operator[] ( const Key & k )"Returns the value associated with the key \fIk.\fR If no such key is present then an empty item is inserted with this key and a reference to the item is returned..PPYou can use this operator in two directions: For reading and for writing:.PP.nf.br QMap<QString,QString> map;.br map[ "Weis" ] = "Torben";.br stream << map[ "Weis" ];.fi.SH "const T& QMap::operator[] ( const Key & k ) const"Returns the value associated with the key \fIk.\fR If no such key is present then a reference to an empty item is returned..SH "void QMap::remove ( Iterator it )"Removes the item at position \fIit\fR in the map..PPSee also clear() and QMapIterator..SH "void QMap::remove ( const Key & k )"Removes the item with the key \fIk.\fR.PPSee also clear()..SH "Iterator QMap::replace ( const Key & k, const T & v )"Replaces the value with key \fIk\fR from the map if possible and inserts the new value \fIv\fR with key \fIk\fR in the map..PPSee also insert(), remove() and QMapIterator..SH RELATED FUNCTION DOCUMENTATION.SH "QDataStream& operator>> (QDataStream & s, QMap<Key,T> & m)"Reads a map from the stream. The types \fIKey\fR and \fIT\fR must implement the streaming operator, too..SH "QDataStream& operator<< (QDataStream & s, const QMap<Key,T> & m)"Writes a map to the stream. The types \fIKey\fR and \fIT\fR must implementthe streaming operator, too..SH "SEE ALSO".BR http://doc.trolltech.com/qmap.html.BR http://www.trolltech.com/faq/tech.html.SH COPYRIGHTCopyright 1992-2001 Trolltech AS, http://www.trolltech.com. See thelicense file included in the distribution for a complete licensestatement..SH AUTHORGenerated automatically from the source code..SH BUGSIf you find a bug in Qt, please report it as described in.BR http://doc.trolltech.com/bughowto.html .Good bug reports make our job much simpler. Thank you..PIn case of content or formattting problems with this manual page, pleasereport them to.BR qt-bugs@trolltech.com .Please include the name of the manual page (qmap.3qt) and the Qtversion (2.3.7).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -