Files
boost_utility/counting_iterator_example.cpp

72 lines
2.3 KiB
C++
Raw Normal View History

2001-02-12 21:35:20 +00:00
// (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
#include <boost/config.hpp>
#include <algorithm>
2001-02-12 21:35:20 +00:00
#include <iostream>
2001-02-12 21:57:19 +00:00
#include <iterator>
#include <vector>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/iterator/indirect_iterator.hpp>
2001-02-12 21:35:20 +00:00
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
namespace boost { namespace detail
{
template <>
struct iterator_traits<int*>
: ptr_iter_traits<int>
{
};
template <>
struct iterator_traits<int**>
: ptr_iter_traits<int*>
{
};
}}
#endif
2001-02-12 21:35:20 +00:00
int main(int, char*[])
{
// Example of using counting_iterator_generator
std::cout << "counting from 0 to 4:" << std::endl;
boost::counting_iterator<int> first(0), last(4);
2001-02-12 21:35:20 +00:00
std::copy(first, last, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// Example of using make_counting_iterator()
2001-02-12 21:57:19 +00:00
std::cout << "counting from -5 to 4:" << std::endl;
2001-02-12 21:35:20 +00:00
std::copy(boost::make_counting_iterator(-5),
2002-02-04 20:29:35 +00:00
boost::make_counting_iterator(5),
std::ostream_iterator<int>(std::cout, " "));
2001-02-12 21:35:20 +00:00
std::cout << std::endl;
2001-02-12 21:57:19 +00:00
// Example of using counting iterator to create an array of pointers.
const int N = 7;
std::vector<int> numbers;
// Fill "numbers" array with [0,N)
std::copy(boost::make_counting_iterator(0), boost::make_counting_iterator(N),
2002-02-04 20:29:35 +00:00
std::back_inserter(numbers));
std::vector<std::vector<int>::iterator> pointers;
2001-02-12 21:35:20 +00:00
2001-02-12 21:57:19 +00:00
// Use counting iterator to fill in the array of pointers.
2002-07-13 12:22:51 +00:00
// causes an ICE with MSVC6
2001-02-12 21:57:19 +00:00
std::copy(boost::make_counting_iterator(numbers.begin()),
2002-02-04 20:29:35 +00:00
boost::make_counting_iterator(numbers.end()),
std::back_inserter(pointers));
2001-02-12 21:57:19 +00:00
// Use indirect iterator to print out numbers by accessing
// them through the array of pointers.
std::cout << "indirectly printing out the numbers from 0 to "
2002-02-04 20:29:35 +00:00
<< N << std::endl;
2001-02-12 21:57:19 +00:00
std::copy(boost::make_indirect_iterator(pointers.begin()),
2002-02-04 20:29:35 +00:00
boost::make_indirect_iterator(pointers.end()),
std::ostream_iterator<int>(std::cout, " "));
2001-02-12 21:57:19 +00:00
std::cout << std::endl;
2001-02-12 21:35:20 +00:00
return 0;
}