⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http:^^www.xraylith.wisc.edu^~khan^software^stl^stl.newbie.html

📁 This data set contains WWW-pages collected from computer science departments of various universities
💻 HTML
📖 第 1 页 / 共 5 页
字号:
Date: Tue, 05 Nov 1996 00:04:24 GMT
Server: Apache/0.8.14
Content-type: text/html
Content-length: 167642
Last-modified: Fri, 13 Oct 1995 17:25:35 GMT

<! ******************************************************************** ><! *           		STL NEWBIE GUIDE				><! ******************************************************************** ><TITLE> Mumit's STL Newbie guide </title><BODY><H1><A name=index>Mumit's</A><!WA0><!WA0><!WA0><!WA0><!WA0><!WA0><a href=http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html> STL Newbie guide</a></H1><UL><LI><!WA1><!WA1><!WA1><!WA1><!WA1><!WA1><a href=#about>About this document</a><LI><!WA2><!WA2><!WA2><!WA2><!WA2><!WA2><a href=#overview>General Overview</a><LI><!WA3><!WA3><!WA3><!WA3><!WA3><!WA3><a href=#class_howto>What's so special about writing container objects</a>    <UL>    <LI><!WA4><!WA4><!WA4><!WA4><!WA4><!WA4><a href=#class_constr>Class constructors</a>    <LI><!WA5><!WA5><!WA5><!WA5><!WA5><!WA5><a href=#class_oper>Class operators</a>    <LI><!WA6><!WA6><!WA6><!WA6><!WA6><!WA6><a href=#container_internals> <b>*NEW*</b> How do STL containers    and container objects interact?</a>    </UL><LI><!WA7><!WA7><!WA7><!WA7><!WA7><!WA7><a href=#pointers>Pointers and STL</a>    <UL>    <LI><!WA8><!WA8><!WA8><!WA8><!WA8><!WA8><a href=#class_pointer>Gotcha's with storing pointers to objects</a>    <LI><!WA9><!WA9><!WA9><!WA9><!WA9><!WA9><a href=#pointer_wrap>Example of a pointer wrapper for storing in	STL containers</a>    <LI><!WA10><!WA10><!WA10><!WA10><!WA10><!WA10><a href=#pointer_deriv>How do I store derived objects in an STL	container?</a>    <LI><!WA11><!WA11><!WA11><!WA11><!WA11><!WA11><a href=#map_find>Checking for item in a map</a>    <LI><!WA12><!WA12><!WA12><!WA12><!WA12><!WA12><a href=#map_charptr>More on evils of <tt>char*</tt>: Phone-book        example.</a>    </UL><LI><!WA13><!WA13><!WA13><!WA13><!WA13><!WA13><a href=#functions><b>*NEW*</b> Predicates, Comparators and General     Functions</a>    <UL>    <LI><!WA14><!WA14><!WA14><!WA14><!WA14><!WA14><a href=#predicates> Predicates: what and how</a>    <LI><!WA15><!WA15><!WA15><!WA15><!WA15><!WA15><a href=#comparators> Comparators: what and how</a>    <LI><!WA16><!WA16><!WA16><!WA16><!WA16><!WA16><a href=#gen_functions> General Functions: what and how</a>    </UL><LI><!WA17><!WA17><!WA17><!WA17><!WA17><!WA17><a href=#iterators>STL iterators</a>    <UL>    <LI><!WA18><!WA18><!WA18><!WA18><!WA18><!WA18><a href=#iterator_end>what does <tt>iterator::end()</tt> return?</a>    <LI><!WA19><!WA19><!WA19><!WA19><!WA19><!WA19><a href=#const_iterator>Const'ness of iterators</a>    <LI><!WA20><!WA20><!WA20><!WA20><!WA20><!WA20><a href=#iterator_tags><b>*NEW*</b> Using iterator tags</a>    </UL><LI><!WA21><!WA21><!WA21><!WA21><!WA21><!WA21><a href=#misc>Miscellaneous examples/notes</a>    <UL>    <LI><!WA22><!WA22><!WA22><!WA22><!WA22><!WA22><a href=#copy_list>Copy between lists: right way and wrong way</a>    <LI><!WA23><!WA23><!WA23><!WA23><!WA23><!WA23><a href=#copy_map>Copy between maps</a>    <LI><!WA24><!WA24><!WA24><!WA24><!WA24><!WA24><a href=#adaptor>Adaptors in STL: <tt>not1</tt>, <tt>stack</tt>, 	<tt>queue</tt>, etc</a>    <LI><!WA25><!WA25><!WA25><!WA25><!WA25><!WA25><a href=#remove_erase><tt>remove</tt> <em>vs</em> <tt>erase</tt></a>    <LI><!WA26><!WA26><!WA26><!WA26><!WA26><!WA26><a href=#list_of_list><tt>List</tt> of <tt>List</tt>'s in STL</a>    <LI><!WA27><!WA27><!WA27><!WA27><!WA27><!WA27><a href=#sorting>Sorting a container</a>	<ul>	<li> <!WA28><!WA28><!WA28><!WA28><!WA28><!WA28><a href=#sort_list>Sorting a <tt>list</tt> of user defined 	    objects</a>	<li> <!WA29><!WA29><!WA29><!WA29><!WA29><!WA29><a href=#sort_vector>Sorting a <tt>vector</tt> of user defined 	    objects</a>	</ul>    <LI><!WA30><!WA30><!WA30><!WA30><!WA30><!WA30><a href=#shuffle>Shuffling a deck of cards</a>    <LI><!WA31><!WA31><!WA31><!WA31><!WA31><!WA31><a href=#value_type>Deducing type from iterator</a>    </UL><LI><!WA32><!WA32><!WA32><!WA32><!WA32><!WA32><a href=#pers_stl>Persistent STL</a><LI><!WA33><!WA33><!WA33><!WA33><!WA33><!WA33><a href=http://www.xraylith.wisc.edu/~khan/software/stl/os_examples/examples.html>236 examples from ObjectSpace</a><LI><!WA34><!WA34><!WA34><!WA34><!WA34><!WA34><a href=#os_stl>A look at ObjectSpace STL&lt;ToolKit&gt;</a><LI><!WA35><!WA35><!WA35><!WA35><!WA35><!WA35><a href=#gcc_templates>Template instantiation with GCC</a>    <UL>    <LI><!WA36><!WA36><!WA36><!WA36><!WA36><!WA36><a href=#gcc_template_visibility><b>*NEW</b> Visibility of template	definitions</a>    <LI><!WA37><!WA37><!WA37><!WA37><!WA37><!WA37><a href=#gcc_manual>Manual instantiation</a>    <LI><!WA38><!WA38><!WA38><!WA38><!WA38><!WA38><a href=#gcc_repo>Using GCC 2.7.0 template repository 	mechanism</a>    </UL><LI><!WA39><!WA39><!WA39><!WA39><!WA39><!WA39><a href=#resources>Internet resources available</a><LI><!WA40><!WA40><!WA40><!WA40><!WA40><!WA40><a href=#ack>Acknowledgments</a></UL><HR><HR><! ******************************************************************** ><! *           		ABOUT THIS DOCUMENT				><! ******************************************************************** ><a name=about><h1> About this Document</h1></a>I started this document as a wastebasket for my random notes and thoughtson STL when I first started learning and using it heavily during winter of1994. So far I have only included a small subset of my STL.NOTES file here(because it is <b><i>such</i></b> a mess), and hopefully will keep adding to it over the next few weeks.<p>As always, I welcome thoughtful comments, suggestions, corrections, ale,and offers of help via email at <!WA41><!WA41><!WA41><!WA41><!WA41><!WA41><a href="mailto:khan@xraylith.wisc.edu"><em>khan@xraylith.wisc.edu</em></a>.</p><p>Copyright(c) 1995 Mumit Khan<hr><!WA42><!WA42><!WA42><!WA42><!WA42><!WA42><a href=#index>Back to index</a> <hr><! ******************************************************************** ><! *           		GENERAL OVERVIEW      				><! ******************************************************************** ><a name=overview><h1> General overview</h1></a>My primary motivation for starting this guide is to help those who are starting out with STL and I would like to hear from you if you find ituseful. Currently most of this document really deals with issues that are hard to find in manuals, such as how to create containers of pointers,or how to manage manual instantiation of STL template with GCC, etc.<p>I personally use <!WA43><!WA43><!WA43><!WA43><!WA43><!WA43><a href=ftp://prep.ai.mit.edu/pub/gnu/gcc-2.7.0.tar.gz> GCC 2.7.0</a> (combined with Cygnus <!WA44><!WA44><!WA44><!WA44><!WA44><!WA44><a href=ftp://ftp.cygnus.com/pub/g++/gcc-2.7.0-repo.gz> template repository</a> patch) and <!WA45><!WA45><!WA45><!WA45><!WA45><!WA45><a href="mailto:info@objectspace.com">ObjectSpace STL&lt;ToolKit&gt;</a>, so please bear this in mind when you see something that doesn't make sense to your configuration. If youare using GCC 2.7.0 with ObjectSpace STL&lt;ToolKit&gt;, then you shouldget in touch with ObjectSpace for bug fixes (See <!WA46><!WA46><!WA46><!WA46><!WA46><!WA46><a href=#os_stl> here</a> for more info).<hr><!WA47><!WA47><!WA47><!WA47><!WA47><!WA47><a href=#index>Back to index</a><hr><! ******************************************************************** ><! *           	What's so special about writing container objects	><! ******************************************************************** ><a name=class_howto><h1>What's so special about writing container objects</h1></a>STL containers copy the objects for local storage, and typically makeheavy use of the default constructor, copy constructor and assignmentoperator. <p>Here I describe some "general guidelines" (read: <i>this is what I do, yourmileage may vary widely</i>) on what constructors and operators one should define when working with STL.<a name=class_constr><h2>Class constructors</h2></a>For each class X, define the following constructors (unless you're happywith the compiler provided ones of course):<ol><li> default constructor -- <tt>X()</tt>;<li> copy constructor -- <tt>X(const X&amp;)</tt>;    <p>    If you don't have this, STL will use the compiler generated one (ie.,    member-wise copy), and embedded pointers will result in weird bugs.</ol><p>See <!WA48><!WA48><!WA48><!WA48><!WA48><!WA48><a href=#container_internals>here</a> for an in-depth look at why andwhen these are needed.<a name=class_oper><h2>Class operators</h2></a>For each class X, define the following operators (again, unless you're happywith the compiler provided ones of course):<ol><li> <tt>operator= (const X&amp;)</tt>    <p>    Better have this -- same effect as copy constructor.<li> <tt>operator&lt (const X&amp;)</tt>    <p>    if your class X is essentially un-ordered, simply return true or false.    For example, if you have container full of pointers to some other    object, ordering may not make sense either.<li> <tt>operator== (const X&amp;)</tt>    <p>    if your class X is essentially un-ordered, simply return true or false.    For example, if you have container full of pointers to some other    object, ordering may not make sense either.</ol><p>The operators <tt>==</tt> and <tt>&lt</tt> are <em>very</em> important forsorted collections like <tt>set</tt>, <tt>map</tt> etc, and are the reasonwhy storing pointers in STL sorted collections may not work as you would expect. See <!WA49><!WA49><!WA49><!WA49><!WA49><!WA49><a href=#class_pointer> here</a> for some caveats in storingpointers in STL containers. Even though I've shown the 2 operators asmember functions here, they don't need be (which is great, since you canuse somebody else's class without having to modify it). eg., if there isa library class <tt>X</tt> that you cannot modify, you can define theoperators <tt>==</tt> and <tt>&lt;</tt> externally as following:<ul><li> <tt>bool operator== (const X&amp; x1, const X&amp; x2)</tt><li> <tt>bool operator&lt;  (const X&amp; x1, const X&amp; x2)</tt></ul><p>Note that you may not need the &lt; and == operators for some compilers if your code doesn't use the algorithms that need these operators (eg., sorting and such things). The compilers I deal with like to instantiateALL the members when I manually instantiate templates, and so I'm outof luck. GCC 2.7.0 seems to do a much better job of instantiating onlythe needed members.<p>See <!WA50><!WA50><!WA50><!WA50><!WA50><!WA50><a href=#container_internals>here</a> for an in-depth look at why andwhen these are needed.<a name=container_internals><h2>How do STL containers    and container objects interact?</h2></a>One of the most frequent questions regarding STL seems to be of the following form: <br><pre>    What members do I have to define for an object that will live in    an STL container?</pre>To explain this without having to go through the STL source code, let'sexamine how <tt>list&lt;T&gt;</tt> works. Here's a typical <tt>list</tt>usage scenario:<pre>    void f() {                           //   &lt;------- (1)        class X { };        list&lt;X&gt; xlist;                   //   &lt;------- (2)        //        // now populate the list with lots of X's.        //        X x();        xlist.push_back(x);             //   &lt;-------- (3)

⌨️ 快捷键说明

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