Brian Silverman | 5962333 | 2018-08-04 23:36:56 -0700 | [diff] [blame^] | 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 | |
| 7 | #include <boost/config.hpp> |
| 8 | #include <algorithm> |
| 9 | #include <iostream> |
| 10 | #include <iterator> |
| 11 | #include <vector> |
| 12 | #include <boost/iterator/counting_iterator.hpp> |
| 13 | #include <boost/iterator/indirect_iterator.hpp> |
| 14 | #include <boost/cstdlib.hpp> |
| 15 | |
| 16 | int main(int, char*[]) |
| 17 | { |
| 18 | // Example of using counting_iterator |
| 19 | std::cout << "counting from 0 to 4:" << std::endl; |
| 20 | boost::counting_iterator<int> first(0), last(4); |
| 21 | std::copy(first, last, std::ostream_iterator<int>(std::cout, " ")); |
| 22 | std::cout << std::endl; |
| 23 | |
| 24 | // Example of using counting iterator to create an array of pointers. |
| 25 | int N = 7; |
| 26 | std::vector<int> numbers; |
| 27 | typedef std::vector<int>::iterator n_iter; |
| 28 | // Fill "numbers" array with [0,N) |
| 29 | std::copy( |
| 30 | boost::counting_iterator<int>(0) |
| 31 | , boost::counting_iterator<int>(N) |
| 32 | , std::back_inserter(numbers)); |
| 33 | |
| 34 | std::vector<std::vector<int>::iterator> pointers; |
| 35 | |
| 36 | // Use counting iterator to fill in the array of pointers. |
| 37 | // causes an ICE with MSVC6 |
| 38 | std::copy(boost::make_counting_iterator(numbers.begin()), |
| 39 | boost::make_counting_iterator(numbers.end()), |
| 40 | std::back_inserter(pointers)); |
| 41 | |
| 42 | // Use indirect iterator to print out numbers by accessing |
| 43 | // them through the array of pointers. |
| 44 | std::cout << "indirectly printing out the numbers from 0 to " |
| 45 | << N << std::endl; |
| 46 | std::copy(boost::make_indirect_iterator(pointers.begin()), |
| 47 | boost::make_indirect_iterator(pointers.end()), |
| 48 | std::ostream_iterator<int>(std::cout, " ")); |
| 49 | std::cout << std::endl; |
| 50 | |
| 51 | return boost::exit_success; |
| 52 | } |