473,383 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Iterators over nested STL container

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?
}
};
Aug 3 '05 #1
2 3603
Ups, sorry. Seems that my post was too early.
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()
see below. Code attached.
-When I use only the two iterators of the nested containers, I do not have
access to begin() and end().
Thats true. See Stoustrup page 19.3. He stores the container as a pointer as
well.
Indeed it seems that my approach is not well designed. Can somebody help
me out here?


This still stands. Do I have a conceptual error?

I append my new code (hopefully as a working example), and would be glad to
get some opinions.

Regards,
Patrick

#iclude <stdio>
#include <vector>

// 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:
// Pointer to the container
T * container;
// 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 outer_it != it.outer_it || inner_it != it.inner_it;
}

// increment
iterator_2D & operator++()
{
if ( outer_it != container->end() ) ++inner_it;
if ( inner_it == (*outer_it).end() )
{
++outer_it;
if ( outer_it != container->end() )
inner_it = (*outer_it).begin();
}

return * this; // why does this not call the defined derefer above?
}
};

template < class T >
class ComposedContainer
{
public:
typedef T value_type;
typedef std::vector < value_type > inner_container;
typedef std::vector < inner_container > outer_container;

typedef iterator_2D < outer_container > iterator;

private:
outer_container data;

public:

ComposedContainer() {}
ComposedContainer( const size_t & size1, const size_t & size2)
{
resize( size1, size2 );
}
void resize( const size_t & size1, const size_t & size2)
{
data.resize( size1 );
for ( size_t i = 0; i < data.size(); ++i )
data[i].resize( size2 );
}

// element access
inner_container & operator[] ( const size_t pos1 )
{
return data[pos1];
}
const inner_container & operator[] ( const size_t pos1 ) const
{
return data[pos1];
}

// basic iterators
iterator begin()
{
iterator ret;
ret.container = &(this->data);
ret.outer_it = data.begin();
ret.inner_it = (*ret.outer_it).begin();
return ret;
}

iterator end()
{
iterator ret;
ret.container = &(this->data);
ret.outer_it = data.end();
ret.inner_it = (*(ret.outer_it - 1)).end();
return ret;
}

};
int main()
{
ComposedContainer<int> a;
ComposedContainer<int> b(3,3);

for ( size_t i = 0; i < 3; ++i )
for ( size_t j = 0; j < 3; ++j )
b[i][j] = (i + 1) * j;
for ( ComposedContainer<int>::iterator it = b.begin(); it != b.end();
++it )
{
int x = *it;
std::cout << x << std::endl;
}

return 0;
}
Aug 3 '05 #2

"Patrick Kowalzick" <pa***************@mapandguide.de> wrote in message
news:ne********************@proxy.mapandguide.de.. .
Ups, sorry. Seems that my post was too early.
I have two containers (standard library) which are nested, e.g.:

[SNIP]

I just did a quick check and the basic concept seems to be okay. However,
I'd suggest to keep your 2D iterator implementation standard lib compliant
and derive it from std::iterator like this:

template <class T>
class Iterator2D : public std::iterator< std::forward_iterator_tag, T,
ptrdiff_t> {

public:

// These type definitions are necessery to be compliant with the STL
// because it uses iterator traits.
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T& reference;
typedef T* pointer;

......

You'll find an example of implementing your own iterator in Nicolai Josuttis
book chapter 7.5.2

Cheers
Chris
Aug 3 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
3
by: Alexander Stippler | last post by:
Hi, I have to design some two dimensional iterators and I'm not quite sure about the design. I'd like to have the iterators mostly STL-like. The STL does not contain two dimensional iterators, I...
9
by: richard.forrest1 | last post by:
I have a problem with an abstract interface class whose implementation classes need to return different iterator types (but with the same value_types etc). Classes A and B both conform to the...
4
by: Merlin | last post by:
Hi, I am a C++ developer and would like to implement container classes for various types of objects. I use MS Visual C++ to compile my code. Nevertheless I like to write code that is independent...
0
by: D.C.Dunn | last post by:
Please forgive the naivety of my following question. I'm new to C++, and am trying to get to grips with OOP. My question concerns iterators and derived classes. Please note that I don't want to use...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
8
by: pvonnied | last post by:
Hi, Once more a question (sorry for the different e-mail addresses , here @ work we have to use Google to post to newsgroups): If I use an iterator, say from std::map, I initialize it like so:...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
3
by: Jess | last post by:
Hello, Iterators are typically put into five different categories, namely input iterator, output iterator, forward iterator, bidirectional iterator and random iterator. The differences come...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.