Re: What is the best way to iterate over a map? (or any paired container)
In article <1155659131.636157.220110@m79g2000cwm.googlegroups.com>,
Diego Martins <jose.diego@gmail.com> wrote:
[discusion of iterating over the second member of a pair in a container
based on std::pair]
I appreciate your quick answers, but all of them were boost related and
I can't use boost in my software due to project policies.
Anyway, I want to learn how to build an iterator wrapper.
If you want to learn how to write general iterator adaptors I advise
reading the docs to boost::iterator_facade and boost::iterator_adaptor
as they handle many 'gotchas' of iterator construction. These use
friend class to dispatch the proper functions from the interface
functions of an iterator. and provide the needed free relational
operators/functions.
writing your own iterator_facade and iterator_adaptor is not difficult
but tedious and without care a 'gotcha' can occurr. see also the
boost headers containing the actual code for ideas.
once you have a facade and adaptor template then writing specific
special iterators becomes easy.
Example an iterator doing what the subject says is [using
boost::iterator_adaptor. note the small amount of actual code written:
template <class Iter> class second_iterator;
template <class Iter>
struct second_iter_base
{
typedef boost::iterator_adaptor
<
second_iterator<Iter>,
Iter,
typename std::iterator_traits<Iter>::value_type::second_type
> type;
};
template <class Iter>
class second_iterator:public second_iter_base<Iter>::type
{
typedef typename second_iter_base<Iter>::type base_type;
friend class boost::iterator_core_access;
typename base_type::reference dereference() const
{
return base_reference()->second;
}
public:
second_iterator() {}
second_iterator(Iter const &i)
:base_type(i){}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]