473,394 Members | 1,750 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,394 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 1688
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: 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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.