Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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


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

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      11-09-2006
foxx <(E-Mail Removed)> 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2006
Marcus Kwok wrote:
> foxx <(E-Mail Removed)> 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


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      11-09-2006
Victor Bazarov <(E-Mail Removed)> wrote:
> Marcus Kwok wrote:
>> foxx <(E-Mail Removed)> 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
 
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 of vectors, etc foxx C++ 2 11-09-2006 06:47 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