#include #include #include #include #include #include #include #include #include template class first_iter : public boost::iterator_adaptor< first_iter, BaseIterator, boost::use_default, boost::use_default, typename boost::mpl::apply_if< boost::is_const , typename BaseIterator::value_type::first_type const & , typename BaseIterator::value_type::first_type & >::type > { private: struct enabler {}; typedef boost::iterator_adaptor< first_iter, BaseIterator, boost::use_default, boost::use_default, typename boost::mpl::apply_if< boost::is_const , typename BaseIterator::value_type::first_type const & , typename BaseIterator::value_type::first_type & >::type > super_t; public: first_iter() {} explicit first_iter(BaseIterator it) : super_t(it) {} template first_iter(first_iter const & other, typename boost::enable_if< boost::is_convertible, enabler >::type = enabler()) : super_t(other.base()) {} private: friend class boost::iterator_core_access; void increment() { this->base_reference() = this->base() + 1; } typename super_t::reference dereference() const { return this->base_reference()->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 first_iter first_iterator; typedef first_iter const_first_iterator; first_iterator begin_first() { return first_iterator(data.begin()); } first_iterator end_first() { return first_iterator(data.end()); } const_first_iterator begin_first() const { return const_first_iterator(data.begin()); } const_first_iterator end_first() const { return const_first_iterator(data.end()); } private: typedef std::pair data_type; std::vector data; }; int main() { store my_store; store::const_first_iterator const begin = my_store.begin_first(); store::const_first_iterator const end = my_store.end_first(); for (store::const_first_iterator it = begin; it != end; ++it) { std::cout << *it << '\n'; } std::flush(std::cout); return 0; }