• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // (C) Copyright Jeremy Siek 2000-2004.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/config.hpp>
7 #include <vector>
8 #include <iostream>
9 #include <iterator>
10 #include <functional>
11 #include <algorithm>
12 #include <boost/bind/bind.hpp>
13 #include <boost/iterator/indirect_iterator.hpp>
14 
main(int,char * [])15 int main(int, char*[])
16 {
17   char characters[] = "abcdefg";
18   const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
19   char* pointers_to_chars[N];                        // at the end.
20   for (int i = 0; i < N; ++i)
21     pointers_to_chars[i] = &characters[i];
22 
23   // Example of using indirect_iterator
24 
25   boost::indirect_iterator<char**, char>
26     indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
27 
28   std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ","));
29   std::cout << std::endl;
30 
31 
32   // Example of making mutable and constant indirect iterators
33 
34   char mutable_characters[N];
35   char* pointers_to_mutable_chars[N];
36   for (int j = 0; j < N; ++j)
37     pointers_to_mutable_chars[j] = &mutable_characters[j];
38 
39   boost::indirect_iterator<char* const*> mutable_indirect_first(pointers_to_mutable_chars),
40     mutable_indirect_last(pointers_to_mutable_chars + N);
41   boost::indirect_iterator<char* const*, char const> const_indirect_first(pointers_to_chars),
42     const_indirect_last(pointers_to_chars + N);
43 
44   std::transform(const_indirect_first, const_indirect_last,
45                  mutable_indirect_first, boost::bind(std::plus<char>(), 1, boost::placeholders::_1));
46 
47   std::copy(mutable_indirect_first, mutable_indirect_last,
48             std::ostream_iterator<char>(std::cout, ","));
49   std::cout << std::endl;
50 
51 
52   // Example of using make_indirect_iterator()
53 
54   std::copy(boost::make_indirect_iterator(pointers_to_chars),
55             boost::make_indirect_iterator(pointers_to_chars + N),
56             std::ostream_iterator<char>(std::cout, ","));
57   std::cout << std::endl;
58 
59   return 0;
60 }
61