473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Iterate over a vector or vectors, etc

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
4 2721
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
9
by: Nancy Keuss | last post by:
Hi, I've created a vector of vectors of ints, and I want to pass it as a parameter to a function. Is this possible, and if so, then what is the syntax like for the function header and function...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
2
by: foxx | last post by:
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...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: Efrat Regev | last post by:
Hello, Let's say a probability vector of dimension d is x_1, ..., x_d, where each one is a non-negative term, and they all sum up to 1. Now I'd like to iterate over all probability vectors, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.