]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/iterator/example/counting_iterator_example.cpp
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)
7 #include <boost/config.hpp>
12 #include <boost/iterator/counting_iterator.hpp>
13 #include <boost/iterator/indirect_iterator.hpp>
14 #include <boost/cstdlib.hpp>
16 int main(int, char*[])
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
;
24 // Example of using counting iterator to create an array of pointers.
26 std::vector
<int> numbers
;
27 typedef std::vector
<int>::iterator n_iter
;
28 // Fill "numbers" array with [0,N)
30 boost::counting_iterator
<int>(0)
31 , boost::counting_iterator
<int>(N
)
32 , std::back_inserter(numbers
));
34 std::vector
<std::vector
<int>::iterator
> pointers
;
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
));
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 "
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
;
51 return boost::exit_success
;