Iterate over a vector of 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 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?


    No, you need a visitor functor:

    struct myvectorvisitor {
    void operator()(const vector<int>& vi) const
    {
    for_each(vi.begin(), vi.end(), call_a_function);
    }
    };

    ...
    vector<vector<int> > vvi;
    for_each(vvi.begin(), vvi.end(), myvectorvisitor());

    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
    #2
    1. Advertising

  3. foxx

    Mark P 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?
    >


    There's nothing built-in but you could make one yourself. Here's an
    untested, incomplete, and very rough sketch of such an iterator. In the
    real world you'd probably want to templatize it, make it more robust,
    and maybe imbue it with the properties of standard library iterators so
    that it could be used with other std. lib. component.

    typedef std::vector<std::vector<int> > VecVecInt;

    class VecVecIter
    {
    public:
    VecVecIter( VecVecInt& data) : data( data), cur_vec( data.begin())
    {
    if( cur_vec != data.end())
    cur_int = cur_vec->begin();
    }

    int& operator*()
    {
    return *cur_int;
    }

    VecVecIter& operator++()
    {
    if( cur_int != cur_vec.end())
    ++cur_int;
    else
    {
    ++cur_vec;
    cur_int = cur_vec->begin();
    }
    }

    bool atEnd() const
    {
    return cur_vec == data.end();
    }

    private:
    VecVecInt& data;
    VecVecInt::iterator cur_vec;
    std::vector<int>::iterator cur_int;
    };
     
    Mark P, Nov 9, 2006
    #3
    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:
    2,002
    Csaba
    Feb 18, 2006
  2. foxx
    Replies:
    4
    Views:
    611
    Marcus Kwok
    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:
    456
    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:
    361
  5. Guest
    Replies:
    0
    Views:
    484
    Guest
    Sep 14, 2005
Loading...

Share This Page