1

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.

Patryk
  • 22,602
  • 44
  • 128
  • 244
  • 3
    I think you accidentally a word in your last sentence. – André Caron Feb 12 '12 at 03:26
  • There just might be a missing – Kimmeh Feb 12 '12 at 03:27
  • 1
    If you are doing this shifting quite a bit, you would be better of with std::vector – Michael Chinen Feb 12 '12 at 03:29
  • Are you asking how to delete the one, or take the found element and insert it into another array? – Dan Feb 12 '12 at 03:29
  • @Dan I do not really care. Either way would work for me. – Patryk Feb 12 '12 at 03:32
  • **Why** can you not use standard library containers? Can you use [`remove_copy_if()`](http://en.cppreference.com/w/cpp/algorithm/remove_copy)? What is the underlying problem you are trying to solve? – johnsyweb Feb 12 '12 at 03:46
  • I am writing an application for Windows Mobile and I have encountered a problem where I cannot use STL containers. As soon as I add code with them I get PInvoke (I have my app in C# and dll written in C++). [This is my question on SO](http://stackoverflow.com/questions/8874059/stl-containers-support-for-windows-mobile) – Patryk Feb 12 '12 at 03:51

5 Answers5

1

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

Dan
  • 1,041
  • 1
  • 12
  • 32
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.
Amy
  • 1,814
  • 2
  • 23
  • 38
1

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.

vvnraman
  • 1,322
  • 13
  • 21
0

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

Community
  • 1
  • 1
Joe Bane
  • 1,556
  • 1
  • 15
  • 30
  • 1
    Thanks Joe but I cannot use any STL containers. I will have a look at this question though. – Patryk Feb 12 '12 at 03:41
0

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.

syerwin
  • 66
  • 1
  • 5