forked from boostorg/range
130 lines
8.6 KiB
HTML
130 lines
8.6 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Introduction</title>
|
|
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
|
|
<link rel="home" href="../index.html" title="Chapter 1. Range 2.0">
|
|
<link rel="up" href="../index.html" title="Chapter 1. Range 2.0">
|
|
<link rel="prev" href="../index.html" title="Chapter 1. Range 2.0">
|
|
<link rel="next" href="concepts.html" title="Range Concepts">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="concepts.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
|
<a name="range.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a>
|
|
</h2></div></div></div>
|
|
<p>
|
|
Generic algorithms have so far been specified in terms of two or more iterators.
|
|
Two iterators would together form a range of values that the algorithm could
|
|
work on. This leads to a very general interface, but also to a somewhat clumsy
|
|
use of the algorithms with redundant specification of container names. Therefore
|
|
we would like to raise the abstraction level for algorithms so they specify
|
|
their interface in terms of <a class="link" href="concepts.html" title="Range Concepts">Ranges</a> as
|
|
much as possible.
|
|
</p>
|
|
<p>
|
|
The most common form of ranges used throughout the C++ community are standard
|
|
library containers. When writing algorithms however, one often finds it desirable
|
|
for the algorithm to accept other types that offer enough functionality to
|
|
satisfy the needs of the generic code <span class="bold"><strong><span class="emphasis"><em>if a
|
|
suitable layer of indirection is applied</em></span></strong></span> . For example,
|
|
raw arrays are often suitable for use with generic code that works with containers,
|
|
provided a suitable adapter is used. Likewise, null terminated strings can
|
|
be treated as containers of characters, if suitably adapted.
|
|
</p>
|
|
<p>
|
|
This library therefore provides the means to adapt standard-like containers,
|
|
null terminated strings, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pairs</span></code> of
|
|
iterators, and raw arrays (and more), such that the same generic code can work
|
|
with them all. The basic idea is to add another layer of indirection using
|
|
<a href="../../../../../libs/mpl/doc/refmanual/metafunction.html" target="_top">metafunctions</a>
|
|
and free-standing functions so syntactic and/or semantic differences can be
|
|
removed.
|
|
</p>
|
|
<p>
|
|
The main advantages are
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
|
|
<li class="listitem">
|
|
simpler implementation and specification of generic range algorithms
|
|
</li>
|
|
<li class="listitem">
|
|
more flexible, compact and maintainable client code
|
|
</li>
|
|
<li class="listitem">
|
|
safe use of built-in arrays (for legacy code; why else would you use built-in
|
|
arrays?)
|
|
</li>
|
|
</ul></div>
|
|
<a name="range.introduction.example___iterate_over_the_values_in_a_map"></a><h4>
|
|
<a name="id2984506"></a>
|
|
<a class="link" href="introduction.html#range.introduction.example___iterate_over_the_values_in_a_map">Example
|
|
- Iterate over the values in a map</a>
|
|
</h4>
|
|
<p>
|
|
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">;</span>
|
|
<span class="identifier">for_each</span><span class="special">(</span> <span class="identifier">my_map</span> <span class="special">|</span> <span class="identifier">map_values</span><span class="special">,</span> <span class="identifier">fn</span> <span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
</p>
|
|
<a name="range.introduction.example___iterate_over_the_keys_in_a_map"></a><h4>
|
|
<a name="id2984631"></a>
|
|
<a class="link" href="introduction.html#range.introduction.example___iterate_over_the_keys_in_a_map">Example
|
|
- Iterate over the keys in a map</a>
|
|
</h4>
|
|
<p>
|
|
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">;</span>
|
|
<span class="identifier">for_each</span><span class="special">(</span> <span class="identifier">my_map</span> <span class="special">|</span> <span class="identifier">map_keys</span><span class="special">,</span> <span class="identifier">fn</span> <span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
</p>
|
|
<a name="range.introduction.example___push_the_even_values_from_a_map_in_reverse_order_into_the_container__code__phrase_role__identifier__target__phrase___code_"></a><h4>
|
|
<a name="id2982448"></a>
|
|
<a class="link" href="introduction.html#range.introduction.example___push_the_even_values_from_a_map_in_reverse_order_into_the_container__code__phrase_role__identifier__target__phrase___code_">Example
|
|
- Push the even values from a map in reverse order into the container <code class="computeroutput"><span class="identifier">target</span></code></a>
|
|
</h4>
|
|
<p>
|
|
|
|
</p>
|
|
<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
|
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">;</span>
|
|
<span class="comment">// Assume that is_even is a predicate that has been implemented elsewhere...
|
|
</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">target</span><span class="special">,</span> <span class="identifier">my_map</span> <span class="special">|</span> <span class="identifier">map_values</span> <span class="special">|</span> <span class="identifier">filtered</span><span class="special">(</span><span class="identifier">is_even</span><span class="special">())</span> <span class="special">|</span> <span class="identifier">reversed</span><span class="special">);</span>
|
|
</pre>
|
|
<p>
|
|
</p>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2003 -2010 Thorsten Ottosen, Neil Groves<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
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>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="concepts.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|