472,989 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 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 1673
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
2
by: zl2k | last post by:
hi, all I need to use gsl_vector pointer with std::vector but not sure how to free the memory when I don't need it. Here is a piece of the code. =================== std::vector<gsl_vector *...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
3
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.