By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,347 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,347 IT Pros & Developers. It's quick & easy.

Iterate over a vector or vectors, etc

P: n/a
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?

Nov 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 9 '06 #2

P: n/a
foxx <ch*********@gmail.comwrote:
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
Nov 9 '06 #3

P: n/a
Marcus Kwok wrote:
foxx <ch*********@gmail.comwrote:
>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
Nov 9 '06 #4

P: n/a
Victor Bazarov <v.********@comacast.netwrote:
Marcus Kwok wrote:
>foxx <ch*********@gmail.comwrote:
>>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
Nov 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.