Boost C++ Libraries: Ticket #10894: Add an adaptor to iterate a range in pairs of (current element, next element) https://svn.boost.org/trac10/ticket/10894 <p> An often needed functionality is iterating a range and accessing the current and next element of the range. </p> <p> Currently this can be done as follows: </p> <div class="wiki-code"><div class="code"><pre><span class="n">assert</span><span class="p">(</span><span class="n">someVec</span><span class="p">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">);</span> <span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">pair</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_iterator_range</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_zip_iterator</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_tuple</span><span class="p">(</span> <span class="n">someVec</span><span class="p">.</span><span class="n">cbegin</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">next</span><span class="p">(</span><span class="n">someVec</span><span class="p">.</span><span class="n">cbegin</span><span class="p">()))),</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_zip_iterator</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_tuple</span><span class="p">(</span> <span class="n">std</span><span class="o">::</span><span class="n">prev</span><span class="p">(</span><span class="n">someVec</span><span class="p">.</span><span class="n">cend</span><span class="p">()),</span> <span class="n">someVec</span><span class="p">.</span><span class="n">cend</span><span class="p">())))</span> <span class="p">)</span> <span class="p">{</span> <span class="k">auto</span> <span class="n">current</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">get</span><span class="o">&lt;</span><span class="mi">0</span><span class="o">&gt;</span><span class="p">(</span><span class="n">pair</span><span class="p">);</span> <span class="k">auto</span> <span class="n">next</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">get</span><span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">(</span><span class="n">pair</span><span class="p">);</span> <span class="p">}</span> </pre></div></div><p> An adaptor would strongly increase readability: </p> <div class="wiki-code"><div class="code"><pre><span class="n">assert</span><span class="p">(</span><span class="n">someVec</span><span class="p">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">);</span> <span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">adaptors</span><span class="o">::</span><span class="k">operator</span><span class="o">|</span><span class="p">;</span> <span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">pair</span><span class="p">,</span> <span class="n">someVec</span> <span class="o">|</span> <span class="n">boost</span><span class="o">::</span><span class="n">adaptors</span><span class="o">::</span><span class="n">paired</span><span class="p">())</span> <span class="p">{</span> <span class="n">pair</span><span class="p">.</span><span class="n">current</span><span class="p">();</span> <span class="n">pair</span><span class="p">.</span><span class="n">next</span><span class="p">();</span> <span class="p">}</span> </pre></div></div><p> Of course the difference between current and next could be made customizable as well. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10894 Trac 1.4.3 Matthäus Brandl Thu, 21 Jan 2016 08:51:26 GMT <link>https://svn.boost.org/trac10/ticket/10894#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10894#comment:1</guid> <description> <p> This can also be achieved in a more concise way: </p> <div class="wiki-code"><div class="code"><pre><span class="n">boost</span><span class="o">::</span><span class="n">combine</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_iterator_range</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_iterator_range</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> </pre></div></div> </description> <category>Ticket</category> </item> </channel> </rss>