Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Iterate over a vector of vectors, etc

Reply
Thread Tools

Iterate over a vector of vectors, etc

 
 
foxx
Guest
Posts: n/a
 
      11-09-2006
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2006
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


 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      11-09-2006
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;
};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: iterate over vector in leaps Alf P. Steinbach C++ 5 04-12-2010 06:05 PM
Iterate over a vector or vectors, etc foxx C++ 4 11-09-2006 09:08 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments