473,387 Members | 1,463 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,387 software developers and data experts.

two dimensional iterators

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 think. I'm not sure, what is the
best way of design and usage syntax.
Is there a good reference or example online? How could such an iterator look
like for the equivalent of std::vector, lets say my::matrix to "exploit"
the pointers as iterators like std::vector does?

regards,
alex
Jul 19 '05 #1
3 6958
"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote...
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 think. I'm not sure, what is the
best way of design and usage syntax.
The same as with all other iterators, I suppose.
Is there a good reference or example online? How could such an iterator look like for the equivalent of std::vector, lets say my::matrix to "exploit"
the pointers as iterators like std::vector does?


Do you mean that your iterator when dereferenced will return another
iterator? That's the essence of two-dimensionality, as I understand
it. So, what's the big deal?

template<class T> class Iterator
{
...
T& operator*();
};

template<class T> class SuperIterator
{
...
Iterator<T>& operator*();
};

Fill in the '...' with all the usual stuff. Also, make your 'my::matrix'
return a SuperIterator<T> from 'begin' and 'end' member functions. OTOH,
if you implement your iterator as a template, then just make 'my::matrix'
return iterator<iterator<T> > from those member functions.

You say that you "have to" design those iterators. Why do you "have to"?
What's the driving force behind it?

Victor
Jul 19 '05 #2

Alexander Stippler <st**@mathematik.uni-ulm.de> wrote in message
news:3f******@news.uni-ulm.de...
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 think. I'm not sure, what is the
best way of design and usage syntax.
Is there a good reference or example online? How could such an iterator look like for the equivalent of std::vector, lets say my::matrix to "exploit"
the pointers as iterators like std::vector does?


A dereference of an iterator can return another iterator.

Consider this example using std::vector:

#include <iostream>
#include <vector>

int main()
{
typedef std::vector<int>::iterator it_dim2;
typedef std::vector<std::vector<int> >::iterator it_dim1;

std::vector<std::vector<int> > vec2d(3, std::vector<int>(5));

for(it_dim1 it1 = vec2d.begin(); it1 != vec2d.end(); ++it1)
for(it_dim2 it2 = it1->begin(); it2 != it1->end(); ++it2)
std::cout << '[' << it1 - vec2d.begin() << ']'
<< '[' << it2 - it1->begin() << ']'
<< " == " << *it2
<< '\n';

return 0;
}
-Mike

Jul 19 '05 #3
Alexander Stippler <st**@mathematik.uni-ulm.de> wrote in message news:<3f******@news.uni-ulm.de>...
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 think. I'm not sure, what is the
best way of design and usage syntax. [...]


It really depends on what you wish to store in this container.

E.g. you could just use a normal std::vector and add extra begin/end
member functions which take an integer (being the row) which then
return an iterator for that particular row.

That way you can e.g. extract row 7 with:

your::container<value_type> c(...);
std::vector<value_type> v(c.begin(7), c.end(7));

or use an appropriate stl-algorithm...

This way you may also invoke algorithms on the entire container using
begin/end without the row number, or you can iterate from e.g. row 7,
column 3 to row 9 column 10 using:

transform(c.begin(7) + 3, c.begin(9) + 10, ...);

This however will not work if you wish to iterate over a sub
'rectangle' of your container. E.g. if it contains a picture and you
wish to apply a filter to a subsection of this image, you most likely
would like the iteration to go from line n to m and visit columns j to
k.

For this I would suggest introducing an adapter similar to:

const your::subsection& s = c.subsection(n, m, j, k);
transform(s.begin(), s.end(), ...);

Another thing to think about is creating your container from a 1D
structure (which will probably become an issue, as all stl algorithms
are rather one dimensional), e.g. you could create a template
constructor taking 2 iterators and a column width, and it would then
insert it as 2D.

Hope it helps, despite the limited info on what you need to store in
this 2D container and what you intent to use it for...
Jul 19 '05 #4

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

Similar topics

18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Kamil | last post by:
I want to sum elements of a multi-dimensional vector, across each vector, or across each dimension... this is my old code: #define v_ldouble vector<ldouble> #define vv_ldouble...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
15
by: Eric Lilja | last post by:
Hello, in one of my classes I have this member variable: Tile ***tiles_; where Tile is another one of my classes. dimensional array of Tile-pointers (dimensions are of equal sizes). Last, I...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.