xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/doc/xml/manual/algorithms.xml (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*48fb7bfaSmrg<chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
2*48fb7bfaSmrg	 xml:id="std.algorithms" xreflabel="Algorithms">
34fee23f9Smrg<?dbhtml filename="algorithms.html"?>
44fee23f9Smrg
5*48fb7bfaSmrg<info><title>
64fee23f9Smrg  Algorithms
74fee23f9Smrg  <indexterm><primary>Algorithms</primary></indexterm>
84fee23f9Smrg</title>
9*48fb7bfaSmrg  <keywordset>
10*48fb7bfaSmrg    <keyword>ISO C++</keyword>
11*48fb7bfaSmrg    <keyword>library</keyword>
12*48fb7bfaSmrg    <keyword>algorithm</keyword>
13*48fb7bfaSmrg  </keywordset>
14*48fb7bfaSmrg</info>
15*48fb7bfaSmrg
16*48fb7bfaSmrg
174fee23f9Smrg
184fee23f9Smrg<para>
19*48fb7bfaSmrg  The neatest accomplishment of the algorithms section is that all the
204fee23f9Smrg  work is done via iterators, not containers directly.  This means two
214fee23f9Smrg  important things:
224fee23f9Smrg</para>
23*48fb7bfaSmrg<orderedlist inheritnum="ignore" continuation="restarts">
244fee23f9Smrg  <listitem>
254fee23f9Smrg    <para>
264fee23f9Smrg      Anything that behaves like an iterator can be used in one of
274fee23f9Smrg      these algorithms.  Raw pointers make great candidates, thus
284fee23f9Smrg      built-in arrays are fine containers, as well as your own
294fee23f9Smrg      iterators.
304fee23f9Smrg    </para>
314fee23f9Smrg  </listitem>
324fee23f9Smrg  <listitem>
334fee23f9Smrg    <para>
344fee23f9Smrg      The algorithms do not (and cannot) affect the container as a
354fee23f9Smrg      whole; only the things between the two iterator endpoints.  If
364fee23f9Smrg      you pass a range of iterators only enclosing the middle third of
374fee23f9Smrg      a container, then anything outside that range is inviolate.
384fee23f9Smrg    </para>
394fee23f9Smrg  </listitem>
404fee23f9Smrg</orderedlist>
414fee23f9Smrg<para>
424fee23f9Smrg  Even strings can be fed through the algorithms here, although the
434fee23f9Smrg  string class has specialized versions of many of these functions
444fee23f9Smrg  (for example, <code>string::find()</code>).  Most of the examples
454fee23f9Smrg  on this page will use simple arrays of integers as a playground
464fee23f9Smrg  for algorithms, just to keep things simple.  The use of
474fee23f9Smrg  <emphasis>N</emphasis> as a size in the examples is to keep things
484fee23f9Smrg  easy to read but probably won't be valid code.  You can use wrappers
494fee23f9Smrg  such as those described in
50*48fb7bfaSmrg  the <link linkend="std.containers">containers section</link> to keep
514fee23f9Smrg  real code readable.
524fee23f9Smrg</para>
534fee23f9Smrg<para>
544fee23f9Smrg  The single thing that trips people up the most is the definition
554fee23f9Smrg  of <emphasis>range</emphasis> used with iterators; the famous
56*48fb7bfaSmrg  "past-the-end" rule that everybody loves to hate.  The
57*48fb7bfaSmrg  <link linkend="std.iterators">iterators section</link> of this
584fee23f9Smrg    document has a complete explanation of this simple rule that seems
594fee23f9Smrg    to cause so much confusion.  Once you
604fee23f9Smrg    get <emphasis>range</emphasis> into your head (it's not that hard,
614fee23f9Smrg    honest!), then the algorithms are a cakewalk.
624fee23f9Smrg</para>
634fee23f9Smrg
644fee23f9Smrg<!-- Sect1 01 : Non Modifying -->
654fee23f9Smrg
664fee23f9Smrg<!-- Sect1 02 : Mutating -->
67*48fb7bfaSmrg<section xml:id="std.algorithms.mutating" xreflabel="Mutating"><info><title>Mutating</title></info>
684fee23f9Smrg
694fee23f9Smrg
70*48fb7bfaSmrg  <section xml:id="algorithms.mutating.swap" xreflabel="swap"><info><title><function>swap</function></title></info>
71*48fb7bfaSmrg
72*48fb7bfaSmrg
73*48fb7bfaSmrg    <section xml:id="algorithms.swap.specializations" xreflabel="Specializations"><info><title>Specializations</title></info>
74*48fb7bfaSmrg
754fee23f9Smrg
764fee23f9Smrg   <para>If you call <code> std::swap(x,y); </code> where x and y are standard
774fee23f9Smrg      containers, then the call will automatically be replaced by a call to
784fee23f9Smrg      <code> x.swap(y); </code> instead.
794fee23f9Smrg   </para>
804fee23f9Smrg   <para>This allows member functions of each container class to take over, and
814fee23f9Smrg      containers' swap functions should have O(1) complexity according to
82*48fb7bfaSmrg      the standard.  (And while "should" allows implementations to
834fee23f9Smrg      behave otherwise and remain compliant, this implementation does in
844fee23f9Smrg      fact use constant-time swaps.)  This should not be surprising, since
854fee23f9Smrg      for two containers of the same type to swap contents, only some
864fee23f9Smrg      internal pointers to storage need to be exchanged.
874fee23f9Smrg   </para>
884fee23f9Smrg
89*48fb7bfaSmrg    </section>
90*48fb7bfaSmrg  </section>
91*48fb7bfaSmrg</section>
924fee23f9Smrg
934fee23f9Smrg<!-- Sect1 03 : Sorting -->
944fee23f9Smrg
954fee23f9Smrg</chapter>
96