#include #include #include #include #include #include template struct select1st : public std::unary_function { typename PairT::first_type & operator()(PairT & pair) const { return pair.first; } }; template struct select1st : public std::unary_function { typename PairT::first_type const & operator()(PairT const& pair) const { return pair.first; } }; class store { private: typedef std::pair data_type; typedef std::vector vector_type; public: store() { data.push_back(std::make_pair(1, "angus")); data.push_back(std::make_pair(2, "brendan")); data.push_back(std::make_pair(3, "chris")); data.push_back(std::make_pair(4, "david")); } typedef boost::transform_iterator, vector_type::iterator> first_iterator; typedef boost::transform_iterator, vector_type::const_iterator> const_first_iterator; first_iterator begin_first() { return boost::make_transform_iterator( data.begin(), select1st()); } first_iterator end_first() { return boost::make_transform_iterator( data.end(), select1st()); } const_first_iterator begin_first() const { return boost::make_transform_iterator( data.begin(), select1st()); } const_first_iterator end_first() const { return boost::make_transform_iterator( data.end(), select1st()); } private: typedef std::pair data_type; std::vector data; }; int main() { store my_store; store const & my_const_store = my_store; store::const_first_iterator const begin = my_const_store.begin_first(); store::const_first_iterator const end = my_const_store.end_first(); for (store::const_first_iterator it = begin; it != end; ++it) { std::cout << *it << '\n'; } std::flush(std::cout); return 0; }