Iterate over a vector or vectors, etc

Discussion in 'C++' started by foxx, Nov 9, 2006.

  1. foxx

    foxx Guest

    I have 2D data structure, modelled as a vector of vectors of ints.
    I'd like to visit each one of the ints and call a function on them.
    Is there some smart way of doing this without using a double for loop,?
    I mean how could I go about creating a new kind of iterator that knows
    how to transverse all the ints in some sequence; or better still, does
    STL already have such a feature?
     
    foxx, Nov 9, 2006
    #1
    1. Advertising

  2. foxx

    mlimber Guest

    foxx wrote:
    > I have 2D data structure, modelled as a vector of vectors of ints.
    > I'd like to visit each one of the ints and call a function on them.
    > Is there some smart way of doing this without using a double for loop,?
    > I mean how could I go about creating a new kind of iterator that knows
    > how to transverse all the ints in some sequence; or better still, does
    > STL already have such a feature?


    You might be interested in this article (the related figures show up in
    pages 1-11):

    http://www.ddj.com/dept/cpp/184401715?pgno=12

    and/or the library it mentions -- VIGRA.

    Cheers! --M
     
    mlimber, Nov 9, 2006
    #2
    1. Advertising

  3. foxx

    Marcus Kwok Guest

    foxx <> wrote:
    > I have 2D data structure, modelled as a vector of vectors of ints.
    > I'd like to visit each one of the ints and call a function on them.
    > Is there some smart way of doing this without using a double for loop,?
    > I mean how could I go about creating a new kind of iterator that knows
    > how to transverse all the ints in some sequence; or better still, does
    > STL already have such a feature?


    Maybe you could nest calls to std::for_each() (found in <algorithm>),
    but that still will probably boil down to a double for loop.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Nov 9, 2006
    #3
  4. Marcus Kwok wrote:
    > foxx <> wrote:
    >> I have 2D data structure, modelled as a vector of vectors of ints.
    >> I'd like to visit each one of the ints and call a function on them.
    >> Is there some smart way of doing this without using a double for
    >> loop,? I mean how could I go about creating a new kind of iterator
    >> that knows how to transverse all the ints in some sequence; or
    >> better still, does STL already have such a feature?

    >
    > Maybe you could nest calls to std::for_each() (found in <algorithm>),
    > but that still will probably boil down to a double for loop.


    What wouldn't? A recursive head-first handling of the vector?

    The point of 'for_each' would be to allow the compiler to optimize
    it better (and it should be able to).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 9, 2006
    #4
  5. foxx

    Marcus Kwok Guest

    Victor Bazarov <> wrote:
    > Marcus Kwok wrote:
    >> foxx <> wrote:
    >>> I have 2D data structure, modelled as a vector of vectors of ints.
    >>> I'd like to visit each one of the ints and call a function on them.
    >>> Is there some smart way of doing this without using a double for
    >>> loop,? I mean how could I go about creating a new kind of iterator
    >>> that knows how to transverse all the ints in some sequence; or
    >>> better still, does STL already have such a feature?

    >>
    >> Maybe you could nest calls to std::for_each() (found in <algorithm>),
    >> but that still will probably boil down to a double for loop.

    >
    > What wouldn't? A recursive head-first handling of the vector?


    Well, the OP explicitly asked if there was a way to do it "without using
    a double for loop", or if the STL had that feature. I gave a
    suggestion, but with the disclaimer that it might still use a double for
    loop, but at least it would be abstracted away into the for_each() call
    (like your solution).

    > The point of 'for_each' would be to allow the compiler to optimize
    > it better (and it should be able to).


    Agreed, though obviously it may also be possible that the compiler can
    optimize the double for loop in the same way.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Nov 9, 2006
    #5
    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. Replies:
    8
    Views:
    1,987
    Csaba
    Feb 18, 2006
  2. foxx
    Replies:
    2
    Views:
    1,068
    Mark P
    Nov 9, 2006
  3. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    438
    Fredrik Lundh
    Aug 13, 2008
  4. Alf P. Steinbach

    Re: iterate over vector in leaps

    Alf P. Steinbach, Apr 12, 2010, in forum: C++
    Replies:
    5
    Views:
    358
  5. Guest
    Replies:
    0
    Views:
    468
    Guest
    Sep 14, 2005
Loading...

Share This Page