1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Introduction</title> 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="Chapter 1. Range 2.0"> 8<link rel="up" href="../index.html" title="Chapter 1. Range 2.0"> 9<link rel="prev" href="../index.html" title="Chapter 1. Range 2.0"> 10<link rel="next" href="concepts.html" title="Range Concepts"> 11</head> 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> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<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> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="range.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a> 28</h2></div></div></div> 29<p> 30 Generic algorithms have so far been specified in terms of two or more iterators. 31 Two iterators would together form a range of values that the algorithm could 32 work on. This leads to a very general interface, but also to a somewhat clumsy 33 use of the algorithms with redundant specification of container names. Therefore 34 we would like to raise the abstraction level for algorithms so they specify 35 their interface in terms of <a class="link" href="concepts.html" title="Range Concepts">Ranges</a> as 36 much as possible. 37 </p> 38<p> 39 The most common form of ranges used throughout the C++ community are standard 40 library containers. When writing algorithms however, one often finds it desirable 41 for the algorithm to accept other types that offer enough functionality to 42 satisfy the needs of the generic code <span class="bold"><strong><span class="emphasis"><em>if a 43 suitable layer of indirection is applied</em></span></strong></span> . For example, 44 raw arrays are often suitable for use with generic code that works with containers, 45 provided a suitable adapter is used. Likewise, null terminated strings can 46 be treated as containers of characters, if suitably adapted. 47 </p> 48<p> 49 This library therefore provides the means to adapt standard-like containers, 50 null terminated strings, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pairs</span></code> of 51 iterators, and raw arrays (and more), such that the same generic code can work 52 with them all. The basic idea is to add another layer of indirection using 53 <a href="../../../../../libs/mpl/doc/refmanual/metafunction.html" target="_top">metafunctions</a> 54 and free-standing functions so syntactic and/or semantic differences can be 55 removed. 56 </p> 57<p> 58 The main advantages are 59 </p> 60<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 61<li class="listitem"> 62 simpler implementation and specification of generic range algorithms 63 </li> 64<li class="listitem"> 65 more flexible, compact and maintainable client code 66 </li> 67<li class="listitem"> 68 safe use of built-in arrays (for legacy code; why else would you use built-in 69 arrays?) 70 </li> 71</ul></div> 72<h4> 73<a name="range.introduction.h0"></a> 74 <span class="phrase"><a name="range.introduction.example___iterate_over_the_values_in_a_map"></a></span><a class="link" href="introduction.html#range.introduction.example___iterate_over_the_values_in_a_map">Example 75 - Iterate over the values in a map</a> 76 </h4> 77<p> 78</p> 79<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span> 80<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> 81<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> 82</pre> 83<p> 84 </p> 85<h4> 86<a name="range.introduction.h1"></a> 87 <span class="phrase"><a name="range.introduction.example___iterate_over_the_keys_in_a_map"></a></span><a class="link" href="introduction.html#range.introduction.example___iterate_over_the_keys_in_a_map">Example 88 - Iterate over the keys in a map</a> 89 </h4> 90<p> 91</p> 92<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span> 93<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> 94<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> 95</pre> 96<p> 97 </p> 98<h4> 99<a name="range.introduction.h2"></a> 100 <span class="phrase"><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></span><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 101 - Push the even values from a map in reverse order into the container <code class="computeroutput"><span class="identifier">target</span></code></a> 102 </h4> 103<p> 104</p> 105<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span> 106<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> 107<span class="comment">// Assume that is_even is a predicate that has been implemented elsewhere...</span> 108<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> 109</pre> 110<p> 111 </p> 112</div> 113<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 114<td align="left"></td> 115<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen, 116 Neil Groves<p> 117 Distributed under the Boost Software License, Version 1.0. (See accompanying 118 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>) 119 </p> 120</div></td> 121</tr></table> 122<hr> 123<div class="spirit-nav"> 124<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> 125</div> 126</body> 127</html> 128