utility/projection_iterator_example.cpp
2004-07-26 00:32:12 +00:00

104 lines
2.9 KiB
C++

// (C) Copyright Jeremy Siek 2000.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/config.hpp>
#include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <boost/iterator/transform_iterator.hpp>
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 const& 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_record> 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 transform_iterator to print out the names in the
// personnel list using a projection.
boost::transform_iterator<
select_name
, std::list<personnel_record>::iterator
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
, std::string
#endif
>
personnel_first(personnel_list.begin()),
personnel_last(personnel_list.end());
std::copy(personnel_first, personnel_last,
std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << std::endl;
// Example of using transform_iterator with const_iterators to
// assign new ID numbers to the personnel.
boost::transform_iterator<
select_ID, std::list<personnel_record>::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;
}
boost::transform_iterator<
select_ID, std::list<personnel_record>::const_iterator, int const&
>
const_ID_first(personnel_list.begin()),
const_ID_last(personnel_list.end());
std::copy(const_ID_first, const_ID_last,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// Example of using make_const_projection_iterator()
// to print out the names in the personnel list again.
std::copy(
boost::make_transform_iterator<select_name>(personnel_list.begin())
, boost::make_transform_iterator<select_name>(personnel_list.end())
, std::ostream_iterator<std::string>(std::cout, "\n"));
#endif
return 0;
}