471,585 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

std::vector managment question

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.

--
Phoenix

Jul 22 '05 #1
2 1616
Clement RAMBACH wrote:
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());

Jul 22 '05 #2
Rolf Magnus wrote:
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.
--
Phoenix

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bartek d | last post: by
10 posts views Thread by Stefan Höhne | last post: by
6 posts views Thread by jmsanchezdiaz | last post: by
8 posts views Thread by jacek.dziedzic | last post: by
3 posts views Thread by Rune Allnor | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.