Search code examples
c++algorithmvisual-c++visual-c++-2010

Removing element from te array function


Is there any already implemented function that has initialArray, sizeOfInitialArray, outArray, and sizeOfOutArray parameters, takes one array and removes specific element, after it creates new array of the exact-element size, something like this below:

void removeElement(int *initialArray, int sizeOfInitialArray, int *& outArray, int & sizeOfOutArray)
 {
       // some code here
 }

I know I remember there was some function exactly processing the code as I want, buy I forgot the name of that function.

Thanks in advance for your efforts.


Solution

  • please see remove from stl..it will process inplace then you may create a new array and return back..

    NotTested! complete code will be

    #include <algorithm>
    void removeElement(int *initialArray, int sizeOfInitialArray, int ValueToBeRemoved, int      *& outArray, int & sizeOfOutArray)
    {
     int* endOfArray = initialArray+sizeOfInitialArray;
     endOfArray = remove(initialArray,endOfArray,ValueToBeRemoved);
     sizeOfOutArray = (endOfArray - initialArray);
     outArray = new int[sizeOfOutArray];
     memcpy(outArray,initialArray,(endOfArray - initialArray)*sizeof(int));
    }