From 4476a85d55e55e802c01ee16200544918ef252d8 Mon Sep 17 00:00:00 2001 From: nobody Date: Wed, 21 Feb 2001 08:01:53 +0000 Subject: [PATCH] This commit was manufactured by cvs2svn to create branch 'unlabeled-1.1.2'. [SVN r9304] --- projection_iterator_example.cpp | 96 +++++++++++++++++++++++++++++++++ reverse_iterator_example.cpp | 42 +++++++++++++++ transform_iterator_example.cpp | 44 +++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 projection_iterator_example.cpp create mode 100644 reverse_iterator_example.cpp create mode 100644 transform_iterator_example.cpp diff --git a/projection_iterator_example.cpp b/projection_iterator_example.cpp new file mode 100644 index 0000000..5405ced --- /dev/null +++ b/projection_iterator_example.cpp @@ -0,0 +1,96 @@ +// (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 +#include +#include +#include +#include +#include +#include + +struct personnel_record { + personnel_record(std::string n, int id) : m_name(n), m_ID(id) { } + std::string m_name; + int m_ID; +}; + +struct select_name { + typedef personnel_record argument_type; + typedef std::string result_type; + const std::string& operator()(const personnel_record& r) const { + return r.m_name; + } + std::string& operator()(personnel_record& r) const { + return r.m_name; + } +}; + +struct select_ID { + typedef personnel_record argument_type; + typedef int result_type; + const int& operator()(const personnel_record& r) const { + return r.m_ID; + } + int& operator()(personnel_record& r) const { + return r.m_ID; + } +}; + +int main(int, char*[]) +{ + std::list personnel_list; + + personnel_list.push_back(personnel_record("Barney", 13423)); + personnel_list.push_back(personnel_record("Fred", 12343)); + personnel_list.push_back(personnel_record("Wilma", 62454)); + personnel_list.push_back(personnel_record("Betty", 20490)); + + // Example of using projection_iterator_generator + // to print out the names in the personnel list. + + boost::projection_iterator_generator::iterator>::type + personnel_first(personnel_list.begin()), + personnel_last(personnel_list.end()); + + std::copy(personnel_first, personnel_last, + std::ostream_iterator(std::cout, "\n")); + std::cout << std::endl; + + // Example of using projection_iterator_pair_generator + // to assign new ID numbers to the personnel. + + typedef boost::projection_iterator_pair_generator::iterator, + std::list::const_iterator> PairGen; + + PairGen::iterator ID_first(personnel_list.begin()), + ID_last(personnel_list.end()); + + int new_id = 0; + while (ID_first != ID_last) { + *ID_first = new_id++; + ++ID_first; + } + + PairGen::const_iterator const_ID_first(personnel_list.begin()), + const_ID_last(personnel_list.end()); + + std::copy(const_ID_first, const_ID_last, + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + std::cout << std::endl; + + // Example of using make_const_projection_iterator() + // to print out the names in the personnel list again. + + std::copy + (boost::make_const_projection_iterator(personnel_list.begin()), + boost::make_const_projection_iterator(personnel_list.end()), + std::ostream_iterator(std::cout, "\n")); + + return 0; +} diff --git a/reverse_iterator_example.cpp b/reverse_iterator_example.cpp new file mode 100644 index 0000000..40c859f --- /dev/null +++ b/reverse_iterator_example.cpp @@ -0,0 +1,42 @@ +// (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 +#include +#include +#include + +int main(int, char*[]) +{ + char letters[] = "hello world!"; + const int N = sizeof(letters)/sizeof(char) - 1; + std::cout << "original sequence of letters:\t" + << letters << std::endl; + + std::sort(letters, letters + N); + + // Use reverse_iterator_generator to print a sequence + // of letters in reverse order. + + boost::reverse_iterator_generator::type + reverse_letters_first(letters + N), + reverse_letters_last(letters); + + std::cout << "letters in descending order:\t"; + std::copy(reverse_letters_first, reverse_letters_last, + std::ostream_iterator(std::cout)); + std::cout << std::endl; + + // Use make_reverse_iterator() to print the sequence + // of letters in reverse-reverse order. + + std::cout << "letters in ascending order:\t"; + std::copy(boost::make_reverse_iterator(reverse_letters_last), + boost::make_reverse_iterator(reverse_letters_first), + std::ostream_iterator(std::cout)); + std::cout << std::endl; + + return 0; +} diff --git a/transform_iterator_example.cpp b/transform_iterator_example.cpp new file mode 100644 index 0000000..4876e0c --- /dev/null +++ b/transform_iterator_example.cpp @@ -0,0 +1,44 @@ +// (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 +#include +#include +#include + +int +main(int, char*[]) +{ + // This is a simple example of using the transform_iterators class to + // generate iterators that multiply the value returned by dereferencing + // the iterator. In this case we are multiplying by 2. + // Would be cooler to use lambda library in this example. + + int x[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + const int N = sizeof(x)/sizeof(int); + + typedef std::binder1st< std::multiplies > Function; + typedef boost::transform_iterator_generator::type doubling_iterator; + + doubling_iterator i(x, std::bind1st(std::multiplies(), 2)), + i_end(x + N, std::bind1st(std::multiplies(), 2)); + + std::cout << "multiplying the array by 2:" << std::endl; + while (i != i_end) + std::cout << *i++ << " "; + std::cout << std::endl; + + std::cout << "adding 4 to each element in the array:" << std::endl; + + std::copy(boost::make_transform_iterator(x, std::bind1st(std::plus(), 4)), + boost::make_transform_iterator(x + N, std::bind1st(std::plus(), 4)), + std::ostream_iterator(std::cout, " ")); + std::cout << std::endl; + + return 0; +} + +