Dear NG,
I have two containers (standard library) which are nested, e.g.:
std::vector< std::vector <int> > A;
std::list< std::vector<int> > B;
These structures where put in another class which I will name
ComposedContainer<T> for the moment.
For these I want to use a sequential iterator with a "flat" 1D-view. Below I
post my first approach iterator_2D. It is more pseudo code than
code......but,
-How shall I implement the functions
ComposedContainer<T>::begin()
ComposedContainer<T>::end()
-When I use only the two iterators of the nested containers, I do not have
access to begin() and end().
Indeed it seems that my approach is not well designed. Can somebody help me
out here?
Thanks, regards,
Patrick
// Iterator concept
template < class T >
class iterator_2D
{
public:
typedef T outer_container;
typedef outer_container::value_type inner_container;
typedef inner_container::value_type value_type;
typedef outer_container::iterator outer_iterator;
typedef inner_container::iterator inner_iterator;
// some more typedefs are missing
private:
// The iterator data
outer_iterator outer_it;
inner_iterator inner_it;
public:
// dereference
value_type & operator * ()
{
return * inner_it;
}
// not equal
bool operator != ( const iterator_2D & it )
{
return it1 != it.it1 || it2 != it.it2;
}
// increment
iterator_2D & operator++()
{
// This data access is stupid and not valid:
//if ( it1 != data.end() ) ++it2;
if ( it2 == (*it1).end() )
{
++it1;
// Same here for the data access
//if ( it1 != data.end() )
it2 = (*it1).begin();
}
return * this; // why does this not call the defined derefer above?
}
};