3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5 <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../index.html" title="The Boost Algorithm Library">
8 <link rel="up" href="../../algorithm/Misc.html" title="Other Algorithms">
9 <link rel="prev" href="find_backward.html" title="find_backward">
10 <link rel="next" href="hex.html" title="hex">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15 <td align="center"><a href="../../../../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <a accesskey="p" href="find_backward.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../algorithm/Misc.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hex.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h3 class="title">
27 <a name="the_boost_algorithm_library.Misc.gather"></a><a class="link" href="gather.html" title="gather">gather</a>
28 </h3></div></div></div>
30 The header file 'boost/algorithm/gather.hpp' contains two variants of a single
31 algorithm, <code class="computeroutput"><span class="identifier">gather</span></code>.
34 <code class="computeroutput"><span class="identifier">gather</span><span class="special">()</span></code>
35 takes a collection of elements defined by a pair of iterators and moves the
36 ones satisfying a predicate to them to a position (called the pivot) within
37 the sequence. The algorithm is stable. The result is a pair of iterators
38 that contains the items that satisfy the predicate.
41 <a name="the_boost_algorithm_library.Misc.gather.h0"></a>
42 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.interface"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.interface">Interface</a>
45 The function <code class="computeroutput"><span class="identifier">gather</span></code> returns
46 a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code> of iterators that denote the elements
47 that satisfy the predicate.
50 There are two versions; one takes two iterators, and the other takes a range.
54 <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">algorithm</span> <span class="special">{</span>
56 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">BidirectionalIterator</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span><span class="special">></span>
57 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">BidirectionalIterator</span><span class="special">,</span><span class="identifier">BidirectionalIterator</span><span class="special">></span>
58 <span class="identifier">gather</span> <span class="special">(</span> <span class="identifier">BidirectionalIterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">BidirectionalIterator</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">BidirectionalIterator</span> <span class="identifier">pivot</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span> <span class="special">);</span>
60 <span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">BidirectionalRange</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Pred</span><span class="special">></span>
61 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special"><</span><span class="keyword">const</span> <span class="identifier">BidirectionalRange</span><span class="special">>::</span><span class="identifier">type</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special"><</span><span class="keyword">const</span> <span class="identifier">BidirectionalRange</span><span class="special">>::</span><span class="identifier">type</span><span class="special">></span>
62 <span class="identifier">gather</span> <span class="special">(</span> <span class="keyword">const</span> <span class="identifier">BidirectionalRange</span> <span class="special">&</span><span class="identifier">range</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special"><</span><span class="keyword">const</span> <span class="identifier">BidirectionalRange</span><span class="special">>::</span><span class="identifier">type</span> <span class="identifier">pivot</span><span class="special">,</span> <span class="identifier">Pred</span> <span class="identifier">pred</span> <span class="special">);</span>
64 <span class="special">}}</span>
69 <a name="the_boost_algorithm_library.Misc.gather.h1"></a>
70 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.examples"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.examples">Examples</a>
73 Given an sequence containing:
75 <pre class="programlisting"><span class="number">0</span> <span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span> <span class="number">8</span> <span class="number">9</span>
80 a call to gather ( arr, arr + 10, arr + 4, IsEven ) will result in:
84 <pre class="programlisting"><span class="number">1</span> <span class="number">3</span> <span class="number">0</span> <span class="number">2</span> <span class="number">4</span> <span class="number">6</span> <span class="number">8</span> <span class="number">5</span> <span class="number">7</span> <span class="number">9</span>
85 <span class="special">|---|-----|</span>
86 <span class="identifier">first</span> <span class="special">|</span> <span class="identifier">second</span>
87 <span class="identifier">pivot</span>
90 where <code class="computeroutput"><span class="identifier">first</span></code> and <code class="computeroutput"><span class="identifier">second</span></code> are the fields of the pair that
91 is returned by the call.
94 <a name="the_boost_algorithm_library.Misc.gather.h2"></a>
95 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.iterator_requirements"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.iterator_requirements">Iterator
99 <code class="computeroutput"><span class="identifier">gather</span></code> work on bidirectional
100 iterators or better. This requirement comes from the usage of <code class="computeroutput"><span class="identifier">stable_partition</span></code>, which requires bidirectional
101 iterators. Some standard libraries (libstdc++ and libc++, for example) have
102 implementations of <code class="computeroutput"><span class="identifier">stable_partition</span></code>
103 that work with forward iterators. If that is the case, then <code class="computeroutput"><span class="identifier">gather</span></code> will work with forward iterators
107 <a name="the_boost_algorithm_library.Misc.gather.h3"></a>
108 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.storage_requirements"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.storage_requirements">Storage
112 <code class="computeroutput"><span class="identifier">gather</span></code> uses <code class="computeroutput"><span class="identifier">stable_partition</span></code>, which will attempt to
113 allocate temporary memory, but will work in-situ if there is none available.
116 <a name="the_boost_algorithm_library.Misc.gather.h4"></a>
117 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.complexity"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.complexity">Complexity</a>
120 If there is sufficient memory available, the run time is linear: <code class="computeroutput"><span class="identifier">O</span><span class="special">(</span><span class="identifier">N</span><span class="special">)</span></code>
123 If there is not any memory available, then the run time is <code class="computeroutput"><span class="identifier">O</span><span class="special">(</span><span class="identifier">N</span>
124 <span class="identifier">log</span> <span class="identifier">N</span><span class="special">)</span></code>.
127 <a name="the_boost_algorithm_library.Misc.gather.h5"></a>
128 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.exception_safety"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.exception_safety">Exception
132 <a name="the_boost_algorithm_library.Misc.gather.h6"></a>
133 <span class="phrase"><a name="the_boost_algorithm_library.Misc.gather.notes"></a></span><a class="link" href="gather.html#the_boost_algorithm_library.Misc.gather.notes">Notes</a>
136 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
137 <td align="left"></td>
138 <td align="right"><div class="copyright-footer">Copyright © 2010-2012 Marshall Clow<p>
139 Distributed under the Boost Software License, Version 1.0. (See accompanying
140 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
145 <div class="spirit-nav">
146 <a accesskey="p" href="find_backward.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../algorithm/Misc.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="hex.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>