dynamic looping

C

cppaddict

Hi,

I need to a way to loop thru vectors (or some other appropriate collection
type)

using constructs such as:

for (pr = someVector.begin(); pr != someVector.end(); pr++)

SomeFunction(*pr);

OR

for_each(someVector.begin(), someVector.end(), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to do
what I

want. Does anyone know what it is?

Thanks,

cpp
 
P

Peter van Merkerk

I need to a way to loop thru vectors (or some other appropriate
collection
type)

using constructs such as:

for (pr = someVector.begin(); pr != someVector.end(); pr++)

SomeFunction(*pr);

OR

for_each(someVector.begin(), someVector.end(), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's

being looped over. Simply adding to the vector as it's being looped,
however, produces strange results.

Changes to the vector may invalidate the iterators.
But I know there must be *some* easy way to do
what I want. Does anyone know what it is?

Use indexes instead of iterators:

for(int i = 0; i < someVector.size(); ++i)
{
SomeFunction(someVector);
}
 
G

Gianni Mariani

cppaddict said:
Hi,

I need to a way to loop thru vectors (or some other appropriate collection
type)

using constructs such as:

for (pr = someVector.begin(); pr != someVector.end(); pr++)

SomeFunction(*pr);

OR

for_each(someVector.begin(), someVector.end(), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to do
what I

want. Does anyone know what it is?

a) You can use an index

for (int pi = 0; pi < someVector.size(); pi++)

b) You can use std::list<>.


The advantage of a std::list is that you may add or remove elements
without invalidating iterators (that are still in the list). However,
depending on your algorithm you may end up removing an element that is
currently being pointed to be an iterator which means you need to manage
this using an indirection.
 
R

Rob Williscroft

cppaddict wrote in
Hi,

I need to a way to loop thru vectors (or some other appropriate
collection type)

using constructs such as:

for (pr = someVector.begin(); pr != someVector.end(); pr++)

SomeFunction(*pr);

OR

for_each(someVector.begin(), someVector.end(), SomeFunction);

The problem is, I want SomeFunction to be able to add to the vector as
it's

being looped over. Simply adding to the vector as it's being looped,
however,

produces strange results. But I know there must be *some* easy way to
do what I

want. Does anyone know what it is?

You need to pick an appropriate container one that doesen't invalidate
its iterators after your insert/push_back.

You can use std::vector if 1) you have reserved enough space in the
vector, by calling reserve() and 2) you only append with push_back().

Alternativly std::deque<> has many of the properties of std::vector<>,
but in addition you can use push_back() (and also push_front()) without
invalidating iterators, (i.e. no need to call reserve()).

If you need to do inserts (i.e. not at the begining or end) then
you need to use std::list<>.

HTH

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top