std::vector managment question

C

Clement RAMBACH

Hi,

here is my problem:

I have a std::vector< A* >. This vector contains pointers to the objects
A i create (lets say about 4000 items). I do this several times in my
application, and at the end of each pass, i have to delete about 3/4 of
the objects (0x0 is put in the vector at the index of the object
destroyed). The criteria of destruction isn't simple so it is not a
"linear" destruction from the begin of the vector up to its end. So, at
the end of a pass, there will be many holes in the vector which is my
problem.

At the end of 10 passes, the vector will contains only 10000 objects
while its size will be of about 40000. So i want to be able to reassort
the vector at the end of each pass in order to get rid of holes.

My first idea is to simply walk the vector, and when a hole is found,
fill it with an object which is at the end of the vector. Once there is
no more holes, i could do a resize on the vector.
I think it could work but I don't know if it is the more efficient way
to do it.

Is there another object than std::vector which could be more efficient
for this job and simple to manage?
Will it be better to erase each element of the vector when i destroy an
object A?
Any other idea? (algorithm, ...)

Thanks in advance.
 
R

Rolf Magnus

Clement said:
Hi,

here is my problem:

I have a std::vector< A* >. This vector contains pointers to the
objects A i create (lets say about 4000 items). I do this several
times in my application, and at the end of each pass, i have to delete
about 3/4 of the objects (0x0 is put in the vector at the index of the
object destroyed). The criteria of destruction isn't simple so it is
not a "linear" destruction from the begin of the vector up to its end.
So, at the end of a pass, there will be many holes in the vector which
is my problem.

At the end of 10 passes, the vector will contains only 10000 objects
while its size will be of about 40000. So i want to be able to
reassort the vector at the end of each pass in order to get rid of
holes.

My first idea is to simply walk the vector, and when a hole is found,
fill it with an object which is at the end of the vector. Once there
is no more holes, i could do a resize on the vector.
I think it could work but I don't know if it is the more efficient way
to do it.

Is there another object than std::vector which could be more efficient
for this job and simple to manage?
Will it be better to erase each element of the vector when i destroy
an object A?
Any other idea? (algorithm, ...)

This should do the trick:

vec.erase(std::remove(vec.begin(), vec.end(), 0), vec.end());
 
C

Clement RAMBACH

Rolf said:
Clement RAMBACH wrote: [...]
Is there another object than std::vector which could be more efficient
for this job and simple to manage?
Will it be better to erase each element of the vector when i destroy
an object A?
Any other idea? (algorithm, ...)

This should do the trick:

vec.erase(std::remove(vec.begin(), vec.end(), 0), vec.end());
It seems fine. Thanks.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top