two dimensional iterators

Discussion in 'C++' started by Alexander Stippler, Aug 20, 2003.

  1. 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
     
    Alexander Stippler, Aug 20, 2003
    #1
    1. Advertising

  2. "Alexander Stippler" <-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
     
    Victor Bazarov, Aug 20, 2003
    #2
    1. Advertising

  3. Alexander Stippler

    Mike Wahler Guest

    Alexander Stippler <-ulm.de> wrote in message
    news:-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
     
    Mike Wahler, Aug 20, 2003
    #3
  4. Alexander Stippler <-ulm.de> wrote in message news:<-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...
     
    Allan Odgaard, Aug 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    444
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,938
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    667
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    994
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    215
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page