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...