1<chapter xmlns="http://docbook.org/ns/docbook" version="5.0" 2 xml:id="std.util" xreflabel="Utilities"> 3<?dbhtml filename="utilities.html"?> 4 5<info><title> 6 Utilities 7 <indexterm><primary>Utilities</primary></indexterm> 8</title> 9 <keywordset> 10 <keyword>ISO C++</keyword> 11 <keyword>library</keyword> 12 </keywordset> 13</info> 14 15 16 17<!-- Section 01 : Functors --> 18<section xml:id="std.util.functors" xreflabel="Functors"><info><title>Functors</title></info> 19<?dbhtml filename="functors.html"?> 20 21 <para>If you don't know what functors are, you're not alone. Many people 22 get slightly the wrong idea. In the interest of not reinventing 23 the wheel, we will refer you to the introduction to the functor 24 concept written by SGI as part of their STL, in 25 <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://web.archive.org/web/20171225062613/http://www.sgi.com/tech/stl/functors.html">their 26 https://web.archive.org/web/20171225062613/http://www.sgi.com/tech/stl/functors.html</link>. 27 </para> 28</section> 29 30<!-- Section 02 : Pairs --> 31<section xml:id="std.util.pairs" xreflabel="Pairs"><info><title>Pairs</title></info> 32<?dbhtml filename="pairs.html"?> 33 34 <para>The <code>pair<T1,T2></code> is a simple and handy way to 35 carry around a pair of objects. One is of type T1, and another of 36 type T2; they may be the same type, but you don't get anything 37 extra if they are. The two members can be accessed directly, as 38 <code>.first</code> and <code>.second</code>. 39 </para> 40 <para>Construction is simple. The default ctor initializes each member 41 with its respective default ctor. The other simple ctor, 42 </para> 43 <programlisting> 44 pair (const T1& x, const T2& y); 45 </programlisting> 46 <para>does what you think it does, <code>first</code> getting <code>x</code> 47 and <code>second</code> getting <code>y</code>. 48 </para> 49 <para>There is a constructor template for copying pairs of other types: 50 </para> 51 <programlisting> 52 template <class U, class V> pair (const pair<U,V>& p); 53 </programlisting> 54 <para>The compiler will convert as necessary from U to T1 and from 55 V to T2 in order to perform the respective initializations. 56 </para> 57 <para>The comparison operators are done for you. Equality 58 of two <code>pair<T1,T2></code>s is defined as both <code>first</code> 59 members comparing equal and both <code>second</code> members comparing 60 equal; this simply delegates responsibility to the respective 61 <code>operator==</code> functions (for types like MyClass) or builtin 62 comparisons (for types like int, char, etc). 63 </para> 64 <para> 65 The less-than operator is a bit odd the first time you see it. It 66 is defined as evaluating to: 67 </para> 68 <programlisting> 69 x.first < y.first || 70 ( !(y.first < x.first) && x.second < y.second ) 71 </programlisting> 72 <para>The other operators are not defined using the <code>rel_ops</code> 73 functions above, but their semantics are the same. 74 </para> 75 <para>Finally, there is a template function called <function>make_pair</function> 76 that takes two references-to-const objects and returns an 77 instance of a pair instantiated on their respective types: 78 </para> 79 <programlisting> 80 pair<int,MyClass> p = make_pair(4,myobject); 81 </programlisting> 82 83</section> 84 85<!-- Section 03 : Memory --> 86<section xml:id="std.util.memory" xreflabel="Memory"><info><title>Memory</title></info> 87<?dbhtml filename="memory.html"?> 88 89 <para> 90 Memory contains three general areas. First, function and operator 91 calls via <function>new</function> and <function>delete</function> 92 operator or member function calls. Second, allocation via 93 <classname>allocator</classname>. And finally, smart pointer and 94 intelligent pointer abstractions. 95 </para> 96 97 <!-- Section 01 : allocator --> 98 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="xml" href="allocator.xml"> 99 </xi:include> 100 101 <!-- Section 02 : auto_ptr --> 102 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="xml" href="auto_ptr.xml"> 103 </xi:include> 104 105 <!-- Section 03 : shared_ptr --> 106 <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="xml" href="shared_ptr.xml"> 107 </xi:include> 108 109</section> 110 111<!-- Section 04 : Traits --> 112<section xml:id="std.util.traits" xreflabel="Traits"><info><title>Traits</title></info> 113<?dbhtml filename="traits.html"?> 114 115 <para> 116 </para> 117</section> 118 119</chapter> 120