qmap.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 549 行 · 第 1/2 页

HTML
549
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/doc/qmap.doc:41 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>QMap Class</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QMap Class Reference</h1><p>The QMap class is a value-based template class that providesa dictionary.<a href="#details">More...</a><p><tt>#include &lt;<a href="qmap-h.html">qmap.h</a>&gt;</tt><p><a href="qmap-members.html">List of all member functions.</a><h2>Public Members</h2><ul><li><div class=fn>typedef Key&nbsp;<a href="#key_type"><b>key_type</b></a></div></li><li><div class=fn>typedef T&nbsp;<a href="#mapped_type"><b>mapped_type</b></a></div></li><li><div class=fn>typedef QPair&lt;const&nbsp;key_type,&nbsp;mapped_type&gt;&nbsp;<a href="#value_type"><b>value_type</b></a></div></li><li><div class=fn>typedef value_type&nbsp;*&nbsp;<a href="#pointer"><b>pointer</b></a></div></li><li><div class=fn>typedef const&nbsp;value_type&nbsp;*&nbsp;<a href="#const_pointer"><b>const_pointer</b></a></div></li><li><div class=fn>typedef value_type&nbsp;&amp;&nbsp;<a href="#reference"><b>reference</b></a></div></li><li><div class=fn>typedef const&nbsp;value_type&nbsp;&amp;&nbsp;<a href="#const_reference"><b>const_reference</b></a></div></li><li><div class=fn>typedef size_t&nbsp;<a href="#size_type"><b>size_type</b></a></div></li><li><div class=fn>typedef QMapIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#iterator"><b>iterator</b></a></div></li><li><div class=fn>typedef QMapConstIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#const_iterator"><b>const_iterator</b></a></div></li><li><div class=fn>typedef QPair&lt;iterator,&nbsp;bool&gt;&nbsp;<b>insert_pair</b></div></li><li><div class=fn><a href="#QMap"><b>QMap</b></a> ()</div></li><li><div class=fn><a href="#QMap-2"><b>QMap</b></a> ( const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li><li><div class=fn><a href="#QMap-3"><b>QMap</b></a> ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li><li><div class=fn><a href="#~QMap"><b>~QMap</b></a> ()</div></li><li><div class=fn>QMap&lt;Key, T&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li><li><div class=fn>QMap&lt;Key, T&gt; &amp; <a href="#operator-eq-2"><b>operator=</b></a> ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li><li><div class=fn>iterator <a href="#begin"><b>begin</b></a> ()</div></li><li><div class=fn>iterator <a href="#end"><b>end</b></a> ()</div></li><li><div class=fn>const_iterator <a href="#begin-2"><b>begin</b></a> () const</div></li><li><div class=fn>const_iterator <a href="#end-2"><b>end</b></a> () const</div></li><li><div class=fn>iterator <a href="#replace"><b>replace</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k, const&nbsp;T&nbsp;&amp;&nbsp;v )</div></li><li><div class=fn>size_type <a href="#size"><b>size</b></a> () const</div></li><li><div class=fn>bool <a href="#empty"><b>empty</b></a> () const</div></li><li><div class=fn>QPair&lt;iterator, bool&gt; <a href="#insert-2"><b>insert</b></a> ( const&nbsp;value_type&nbsp;&amp;&nbsp;x )</div></li><li><div class=fn>void <a href="#erase"><b>erase</b></a> ( iterator&nbsp;it )</div></li><li><div class=fn>void <a href="#erase-2"><b>erase</b></a> ( const&nbsp;key_type&nbsp;&amp;&nbsp;k )</div></li><li><div class=fn>size_type <a href="#count"><b>count</b></a> ( const&nbsp;key_type&nbsp;&amp;&nbsp;k ) const</div></li><li><div class=fn>T &amp; <a href="#operator[]"><b>operator[]</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</div></li><li><div class=fn>void <a href="#clear"><b>clear</b></a> ()</div></li><li><div class=fn>typedef QMapIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#Iterator"><b>Iterator</b></a></div></li><li><div class=fn>typedef QMapConstIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#ConstIterator"><b>ConstIterator</b></a></div></li><li><div class=fn>typedef T&nbsp;<a href="#ValueType"><b>ValueType</b></a></div></li><li><div class=fn>iterator <a href="#find"><b>find</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</div></li><li><div class=fn>const_iterator <a href="#find-2"><b>find</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</div></li><li><div class=fn>const T &amp; <a href="#operator[]-2"><b>operator[]</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</div></li><li><div class=fn>bool <a href="#contains"><b>contains</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</div></li><li><div class=fn>size_type <a href="#count-2"><b>count</b></a> () const</div></li><li><div class=fn>QValueList&lt;Key&gt; <a href="#keys"><b>keys</b></a> () const</div></li><li><div class=fn>QValueList&lt;T&gt; <a href="#values"><b>values</b></a> () const</div></li><li><div class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</div></li><li><div class=fn>iterator <a href="#insert"><b>insert</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;key, const&nbsp;T&nbsp;&amp;&nbsp;value, bool&nbsp;overwrite = TRUE )</div></li><li><div class=fn>void <a href="#remove"><b>remove</b></a> ( iterator&nbsp;it )</div></li><li><div class=fn>void <a href="#remove-2"><b>remove</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</div></li></ul><h2>Protected Members</h2><ul><li><div class=fn>void <a href="#detach"><b>detach</b></a> ()</div></li></ul><h2>Related Functions</h2><ul><li><div class=fn>QDataStream &amp; <a href="#operator-gt-gt"><b>operator&gt;&gt;</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li><li><div class=fn>QDataStream &amp; <a href="#operator-lt-lt"><b>operator&lt;&lt;</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</div></li></ul><hr><a name="details"></a><h2>Detailed Description</h2><p> The QMap class is a value-based template class that providesa dictionary.<p> <p> QMap is a Qt implementation of an STL-like map container.  Itcan be used in your application if the standard <tt>map</tt> is notavailable.  QMap is part of the <a href="qtl.html">Qt Template  Library</a>.<p> QMap&lt;Key, Data&gt; defines a template instance to create adictionary with keys of type Key and values of type Data. QMap doesnot store pointers to the members of the map; instead, it holds acopy of every member. For that reason, QMap is value-based, whereas<a href="qptrlist.html">QPtrList</a> and <a href="qdict.html">QDict</a> are pointer-based.<p> QMap contains and manages a collection of objects of type Data withassociated key values of type Key and provides iterators that allowthe contained objects to be addressed.  QMap owns the containeditems.<p> Some classes cannot be used within a QMap. For example everythingderived from <a href="qobject.html">QObject</a> and thus all classes that implement widgets.Only values can be used in a QMap. To qualify as a value, the classmust provide<p> <ul><li> A copy constructor<li> An assignment operator<li> A default constructor, i.e. a constructor that does not take any arguments.</ul><p> Note that C++ defaults to field-by-field assignment operators andcopy constructors if no explicit version is supplied. In many cases,this is sufficient.<p> The class used for the key requires that the <tt>operator&lt;</tt> is implementedto define ordering of the keys.<p> QMap's function naming is consistent with the other Qt classes(e.g., <a href="#count">count</a>(), <a href="#isEmpty">isEmpty</a>()). QMap also provides extra functions forcompatibility with STL algorithms, such as <a href="#size">size</a>() and <a href="#empty">empty</a>().Programmers already familiar with the STL <tt>map</tt> can use thesefunctions instead.<p> Example:<a name="qmap-eg"></a><pre>    #include &lt;<a href="qstring-h.html">qstring.h</a>&gt;    #include &lt;<a href="qmap-h.html">qmap.h</a>&gt;    #include &lt;<a href="qstring-h.html">qstring.h</a>&gt;    class Employee    {    public:        Employee(): sn(0) {}        Employee( const <a href="qstring.html">QString</a>&amp; forename, const <a href="qstring.html">QString</a>&amp; surname, int salary )            : fn(forename), sn(surname), sal(salary)        { }        <a href="qstring.html">QString</a> forename() const { return fn; }        <a href="qstring.html">QString</a> surname() const { return sn; }        int salary() const { return sal; }        void setSalary( int salary ) { sal = salary; }    private:        <a href="qstring.html">QString</a> fn;        <a href="qstring.html">QString</a> sn;        int sal;    };    int main(int argc, char **argv)    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        typedef QMap&lt;QString, Employee&gt; EmployeeMap;        EmployeeMap map;        map["JD001"] = Employee("John", "Doe", 50000);        map["JD002"] = Employee("Jane", "Williams", 80000);        map["TJ001"] = Employee("Tom", "Jones", 60000);        Employee sasha( "Sasha", "Hind", 50000 );        map["SH001"] = sasha;        sasha.setSalary( 40000 );        EmployeeMap::<a href="#Iterator">Iterator</a> it;        for ( it = map.begin(); it != map.end(); ++it ) {            printf( "%s: %s, %s earns %d\n",                    it.key().latin1(),                    it.data().surname().latin1(),                    it.data().forename().latin1(),                    it.data().salary() );        }        return 0;    }</pre> <p> Program output:<pre>    JD001: Doe, John earns 50000    JW002: Williams, Jane earns 80000    SH001: Hind, Sasha earns 50000    TJ001: Jones, Tom earns 60000</pre> <p> The latest changes to Sasha's salary did not affect the value inthe list because the map created a copy of Sasha's entry. Inaddition, notice that the items are sorted alphabetically (by key)when iterating over the map.<p> There are several ways to find items in a map.  The <a href="#begin">begin</a>() and<a href="#end">end</a>() functions return iterators to the beginning and end of themap. The advantage of using an iterator is that you can moveforward or backward by incrementing/decrementing the iterator.  Theiterator returned by end() points to the element which is one pastthe last element in the container. The past-the-end iterator isstill associated with the map it belongs to, however it is <em>not</em>dereferenceable; <a href="qwmatrix.html#operator*-5">operator*</a>() will not return a well-defined value.If the map is empty, the iterator returned by begin() will equalthe iterator returned by end().<p> Another way to find an element in the map is by using the <a href="#find">find</a>()function. This returns an iterator pointing to the desireditem or to the end() iterator if no such element exists.<p> Another approach uses the operator[]. But be warned: if the map doesnot contain an entry for the element you are looking for, operator[]inserts a default value.  If you do not know that the element youare searching for is really in the list, you should not useoperator[]. The following example illustrates this:<p> <pre>        QMap&lt;QString,QString&gt; map;        map["Clinton"] = "Bill";        str &lt;&lt; map["Clinton"] &lt;&lt; map["Bush"] &lt;&lt; endl;  </pre> <p> The code fragment will print out "Clinton", "".  Since the valueassociated with the "Bush" key did not exist, the map inserted adefault value (in this case, an empty string).  If you are notsure whether a certain element is in the map, you should use find()and iterators instead.<p> If you just want to know whether a certain key is contained in themap, use the <a href="#contains">contains</a>() function. In addition, <a href="#count">count</a>() tells you howmany keys there are currently in the map.<p> It is safe to have multiple iterators at the same time. If somemember of the map is removed, only iterators pointing to the removedmember become invalid; inserting in the map does not invalidate anyiterators.<p> Since QMap is value-based, there is no need to be concerned about deletingitems in the map. The map holds its own copies and will freethem if the corresponding member or the map itself is deleted.<p> QMap is <a href="shclass.html#implicitly-shared">implicitly shared</a>. This means you can just make copies ofthe map in time O(1). If multiple QMap instances share the same dataand one is modifying the map's data, this modifying instancemakes a copy and modifies its private copy; it thus does not affectother instances.  From a developer's point of view you can thinkthat a QMap and a copy of this map have nothing to do with eachother. If a QMap is being used in a multi-threaded program, you mustprotect all access to the map. See <a href="qmutex.html">QMutex</a>.<p> There are several ways of inserting new items into the map. Oneuses the <a href="#insert">insert</a>() method; the other one uses operator[] like this:<pre>    QMap&lt;QString, QString&gt; map;    map["Clinton"] = "Bill";    map.<a href="#insert">insert</a>( <a href="qpair.html#qMakePair">qMakePair</a>("Bush", "George") );  </pre> <p> Items can also be removed from the map in several ways. The first isto pass an iterator to <a href="#remove">remove</a>(). The other is to pass a keyvalue to remove(), which will delete the entry with the requestedkey. In addition you can clear the entire map using the <a href="#clear">clear</a>()method.<p> <p>See also <a href="qmapiterator.html">QMapIterator</a>, <a href="qtl.html">Qt Template Library Classes</a>, <a href="shared.html">Implicitly and Explicitly Shared Classes</a> and <a href="tools.html">Non-GUI Classes</a>.<hr><h2>Member Type Documentation</h2><h3 class=fn><a name="ConstIterator"></a>QMap::ConstIterator</h3> The map's const iterator type, Qt style.<h3 class=fn><a name="Iterator"></a>QMap::Iterator</h3> The map's iterator type, Qt style. <h3 class=fn><a name="ValueType"></a>QMap::ValueType</h3> Corresponds to <a href="qpair.html">QPair</a>&lt;key_type, mapped_type&gt;, Qt style.<h3 class=fn><a name="const_iterator"></a>QMap::const_iterator</h3> The map's const iterator type.<h3 class=fn><a name="const_pointer"></a>QMap::const_pointer</h3> Const pointer to value_type.<h3 class=fn><a name="const_reference"></a>QMap::const_reference</h3> 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?