How to remove elements from dynamically allocated array?

北战南征 提交于 2019-12-13 02:25:09

问题


I have a dynamically allocated array :

myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel];

I would like to loop through all elements in this array and remove these that will meet my condition (e.g. too big rectangle).

I have been thinking that I can loop through this array and get the number of elements that would satisfy my condition and then allocate a new array. But how can I 'transfer' these 'wanted' elements into my new array ?

Just for the record: I cannot use STL containers.


回答1:


myRectangle * lastRectanglesArray = new myRectangle[lastMaxLabel];
// initialize the entries in the lastRectanglesArray

// create a temporary array which contains info about each individual
// entry. namely, it only holds info about whether the entry should
// be kept, or deleted.
// we also use the 'entries' value, which is the number of entries
// in the new array
bool * entriesToKeep = new bool[lastMaxLabel];
int entries = 0;

// check each entry, and mark whether it should be kept or deleted
for (int i = 0; i != lastMaxLabel; ++i) {
    // check whether the entry should be kept or deleted...
    // here, i just put a function with signature like:
    // bool shouldKeepRectangle(const myRectangle &);
    entriesToKeep[i] = shouldKeepRectangle(lastRectanglesArray[i]);
    if (entriesToKeep[i]) ++entries;
}

// create a new array that will contain the entries that should be kept
myRectangle * rectanglesArray = new myRectangle[entries];

// assign the entries in the new array
for (int i = 0, j = 0; i != lastMaxLabel && j != entries; ++i) {
    if (entriesToKeep[i])
        rectanglesArray[j++] = lastRectanglesArray[i];
}

// free the memory held by the temp array
delete [] entriesToKeep;

// if the old array is not needed anymore, delete it
delete [] lastRectanglesArray;

// and here you have rectanglesArray, a brand new array that contains
// only the elements that you need.



回答2:


Just move the next array location over the one that needs to be deleted, and shift everything over til the end of the array.




回答3:


Yours look like the perfect case for using a Linked List. You would however have to do away with the new myRectangle[lastMaxLabel] part as you would have to implement it as pert of your Insert() function.

This way you would not require to transfer the wanted elements into a new array, but just delete the unwanted element.

Any more light on your use-case would help us to think of better alternatives.




回答4:


I agree with Michael Chinen - use std::vector instead. You'll avoid lots of other potential problems this way. If you really want to use dynamic arrays, see this question: Remove an array element and shift the remaining ones




回答5:


if you have a big amount of data in array that will be a problem for shifting using loop

maybe you should build your own array management class (find,add,deleteAt,etc).

my suggestion use link list node method.. it will be faster rather then you use loop for shifting.



来源:https://stackoverflow.com/questions/9246165/how-to-remove-elements-from-dynamically-allocated-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!