• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Copyright David Abrahams 2006. Distributed under the Boost
2.. Software License, Version 1.0. (See accompanying
3.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5Example
6.......
7
8This example fills an array with numbers and a second array with
9pointers into the first array, using ``counting_iterator`` for both
10tasks. Finally ``indirect_iterator`` is used to print out the numbers
11into the first array via indirection through the second array.
12
13::
14
15    int N = 7;
16    std::vector<int> numbers;
17    typedef std::vector<int>::iterator n_iter;
18    std::copy(boost::counting_iterator<int>(0),
19             boost::counting_iterator<int>(N),
20             std::back_inserter(numbers));
21
22    std::vector<std::vector<int>::iterator> pointers;
23    std::copy(boost::make_counting_iterator(numbers.begin()),
24	      boost::make_counting_iterator(numbers.end()),
25	      std::back_inserter(pointers));
26
27    std::cout << "indirectly printing out the numbers from 0 to "
28	      << N << std::endl;
29    std::copy(boost::make_indirect_iterator(pointers.begin()),
30	      boost::make_indirect_iterator(pointers.end()),
31	      std::ostream_iterator<int>(std::cout, " "));
32    std::cout << std::endl;
33
34
35The output is::
36
37    indirectly printing out the numbers from 0 to 7
38    0 1 2 3 4 5 6
39
40The source code for this example can be found `here`__.
41
42__ ../example/counting_iterator_example.cpp
43
44