counting_iterator_eg.rst 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. Example
  5. .......
  6. This example fills an array with numbers and a second array with
  7. pointers into the first array, using ``counting_iterator`` for both
  8. tasks. Finally ``indirect_iterator`` is used to print out the numbers
  9. into the first array via indirection through the second array.
  10. ::
  11. int N = 7;
  12. std::vector<int> numbers;
  13. typedef std::vector<int>::iterator n_iter;
  14. std::copy(boost::counting_iterator<int>(0),
  15. boost::counting_iterator<int>(N),
  16. std::back_inserter(numbers));
  17. std::vector<std::vector<int>::iterator> pointers;
  18. std::copy(boost::make_counting_iterator(numbers.begin()),
  19. boost::make_counting_iterator(numbers.end()),
  20. std::back_inserter(pointers));
  21. std::cout << "indirectly printing out the numbers from 0 to "
  22. << N << std::endl;
  23. std::copy(boost::make_indirect_iterator(pointers.begin()),
  24. boost::make_indirect_iterator(pointers.end()),
  25. std::ostream_iterator<int>(std::cout, " "));
  26. std::cout << std::endl;
  27. The output is::
  28. indirectly printing out the numbers from 0 to 7
  29. 0 1 2 3 4 5 6
  30. The source code for this example can be found `here`__.
  31. __ ../example/counting_iterator_example.cpp