Remove all elements from array greater than n

I am starting to program. Something is causing me problems with the code. Let's say I have an array.

int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

      

I want to delete all items that are greater than 9. How can I do this?

+3


source to share


3 answers


You can do this if you are using a vector. First, initialize the vector with your array. Then use the remove_if () function. Hope this helps.



#include <algorithm>
#include <vector>

int main()
{
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

vector<int> V(Array, Array+20);
vector<int> :: iterator it;

it = remove_if(V.begin(), V.end(), bind2nd(greater<int>(), 9));


V.erase (it, V.end());  // This is your required vector if you wish to use vector

}

      

+7


source


You cannot remove elements from an array as they are fixed in size.

If you have used std::vector

then the solution will look like this:

  #include <vector>
  #include <algorithm>
  #include <iostream>
  #include <iterator>

  using namespace std;

  int main()
  {
     std::vector<int> Array = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

     Array.erase(remove_if(Array.begin(), Array.end(), [](int n) { return n > 9; }),
                 Array.end());
     copy(Array.begin(), Array.end(), ostream_iterator<int>(cout, " "));

  }

      

Live example: http://ideone.com/UjdJ5h

If you want to stick to your array, but mark the elements that are greater than 10, you can use the same algorithm std::remove_if

.



  #include <algorithm>
  #include <iostream>
  #include <iterator>

  using namespace std;

  int main()
  {
     int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
     int *overwrite_start = remove_if(std::begin(Array), std::end(Array), [](int n){ return n>9; });
     fill(overwrite_start, std::end(Array), -1);
     copy(std::begin(Array), std::end(Array), ostream_iterator<int>(cout, " "));
  }

      

The above will move the "erased" elements to the end of the array and mark them -1.

Live example: http://ideone.com/7rwaXy

Note the use of the STL algorithm in both function examples. The second array example uses the same algorithm function remove_if

. remove_if

returns the beginning of the "erased" data, since remove_if

it does not actually delete, but moves the data to the end of the sequence.

+5


source


I am trying to replace the swap concept without using a vector

int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len = sizeof(Array)/sizeof(int);
void print_array_value() {
int i;
cout << "\n";
for (i = 0; i < arr_len; i++) {
    cout << Array[i] << ", ";
}
cout << " : " << arr_len << "\n";
}
void swap_array_value(int start) {
int i;
for ( ; (start+1) < arr_len; start++) {
    Array[start] = Array[start+1];
}
}
void remove_array_value() {
int i;
for (i = 0; i < arr_len; i++) {
    if (Array[i] > n) {
        swap_array_value(i);
        arr_len--;
        i--;
    }
}
}
void main () {
clrscr();
cout << "Enter the N value : ";
cin >> n;
print_array_value();
remove_array_value();
print_array_value();
cout << "Array Length : " << arr_len;
getch();
}

      

+1


source







All Articles