forked from boostorg/iterator
added make_xxx functions for some of the iterators
[SVN r21611]
This commit is contained in:
54
example/counting_iterator_example.cpp
Normal file
54
example/counting_iterator_example.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
// (C) Copyright Jeremy Siek 2000-2004.
|
||||
// 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>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <boost/iterator/indirect_iterator.hpp>
|
||||
#include <boost/cstdlib.hpp>
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
// Example of using counting_iterator
|
||||
std::cout << "counting from 0 to 4:" << std::endl;
|
||||
boost::counting_iterator<int> first(0), last(4);
|
||||
std::copy(first, last, std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example of using counting iterator to create an array of pointers.
|
||||
int N = 7;
|
||||
std::vector<int> numbers;
|
||||
typedef std::vector<int>::iterator n_iter;
|
||||
// Fill "numbers" array with [0,N)
|
||||
std::copy(
|
||||
boost::counting_iterator<int>(0)
|
||||
, boost::counting_iterator<int>(N)
|
||||
, std::back_inserter(numbers));
|
||||
|
||||
std::vector<std::vector<int>::iterator> pointers;
|
||||
|
||||
// Use counting iterator to fill in the array of pointers.
|
||||
// causes an ICE with MSVC6
|
||||
std::copy(boost::make_counting_iterator(numbers.begin()),
|
||||
boost::make_counting_iterator(numbers.end()),
|
||||
std::back_inserter(pointers));
|
||||
|
||||
// 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 "
|
||||
<< N << std::endl;
|
||||
std::copy(boost::make_indirect_iterator(pointers.begin()),
|
||||
boost::make_indirect_iterator(pointers.end()),
|
||||
std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
return boost::exit_success;
|
||||
}
|
60
example/filter_iterator_example.cpp
Normal file
60
example/filter_iterator_example.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// (C) Copyright Jeremy Siek 1999-2004.
|
||||
// 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>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <boost/iterator/filter_iterator.hpp>
|
||||
#include <boost/cstdlib.hpp> // for exit_success
|
||||
|
||||
struct is_positive_number {
|
||||
bool operator()(int x) { return 0 < x; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int numbers_[] = { 0, -1, 4, -3, 5, 8, -2 };
|
||||
const int N = sizeof(numbers_)/sizeof(int);
|
||||
|
||||
typedef int* base_iterator;
|
||||
base_iterator numbers(numbers_);
|
||||
|
||||
// Example using make_filter_iterator()
|
||||
std::copy(boost::make_filter_iterator<is_positive_number>(numbers, numbers + N),
|
||||
boost::make_filter_iterator<is_positive_number>(numbers + N, numbers + N),
|
||||
std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
// Example using filter_iterator
|
||||
typedef boost::filter_iterator<is_positive_number, base_iterator>
|
||||
FilterIter;
|
||||
|
||||
is_positive_number predicate;
|
||||
FilterIter filter_iter_first(predicate, numbers, numbers + N);
|
||||
FilterIter filter_iter_last(predicate, numbers + N, numbers + N);
|
||||
|
||||
std::copy(filter_iter_first, filter_iter_last, std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
// Another example using make_filter_iterator()
|
||||
std::copy(
|
||||
boost::make_filter_iterator(
|
||||
std::bind2nd(std::greater<int>(), -2)
|
||||
, numbers, numbers + N)
|
||||
|
||||
, boost::make_filter_iterator(
|
||||
std::bind2nd(std::greater<int>(), -2)
|
||||
, numbers + N, numbers + N)
|
||||
|
||||
, std::ostream_iterator<int>(std::cout, " ")
|
||||
);
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
return boost::exit_success;
|
||||
}
|
43
example/reverse_iterator_example.cpp
Normal file
43
example/reverse_iterator_example.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
// (C) Copyright Jeremy Siek 2000-2004.
|
||||
// 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 <iostream>
|
||||
#include <algorithm>
|
||||
#include <boost/iterator/reverse_iterator.hpp>
|
||||
#include <boost/cstdlib.hpp>
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
char letters_[] = "hello world!";
|
||||
const int N = sizeof(letters_)/sizeof(char) - 1;
|
||||
typedef char* base_iterator;
|
||||
base_iterator letters(letters_);
|
||||
|
||||
std::cout << "original sequence of letters:\t\t\t"
|
||||
<< letters_ << std::endl;
|
||||
|
||||
// Use reverse_iterator to print a sequence of letters in reverse
|
||||
// order.
|
||||
|
||||
boost::reverse_iterator<base_iterator>
|
||||
reverse_letters_first(letters + N),
|
||||
reverse_letters_last(letters);
|
||||
|
||||
std::cout << "sequence in reverse order:\t\t\t";
|
||||
std::copy(reverse_letters_first, reverse_letters_last,
|
||||
std::ostream_iterator<char>(std::cout));
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "sequence in double-reversed (normal) order:\t";
|
||||
std::copy(boost::make_reverse_iterator(reverse_letters_last),
|
||||
boost::make_reverse_iterator(reverse_letters_first),
|
||||
std::ostream_iterator<char>(std::cout));
|
||||
std::cout << std::endl;
|
||||
|
||||
return boost::exit_success;
|
||||
}
|
Reference in New Issue
Block a user