Remove all elements from array greater than n
Asked Answered
G

3

5

I'm beginner in programming. Something is giving me trouble to code. Suppose, I've 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 remove all elements which are greater than 9. How can I do this?

Gidgetgie answered 17/12, 2014 at 7:43 Comment(4)
You can't remove elements from an array. The best you can do is set them to some special value.Offenseless
You have trouble coding it because it is impossible to remove items from an array.Sheldonshelduck
possible duplicate of Delete element from C++ arrayPayne
@trejder, thank you. But i want to remove element in conditionGidgetgie
A
11

You can do this if you use vector. First initialize vector with your array. Then use remove_if() function. Hope this will help.

#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

}
Abelmosk answered 17/12, 2014 at 7:51 Comment(3)
If it is not possible to remove element from array, then i will try to use this.. Thank you.Gidgetgie
Why the int K? That is not necessary. V.erase(it, V.end());Sheldonshelduck
@PaulMcKenzie, Oh.. tnx.Abelmosk
S
7

You cannot remove items from an array, since they are fixed in size.

If you used std::vector, then the solution would 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 with your array, but mark the items 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" items to the end of the array, and mark them with -1.

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

Note the usage in both examples of the STL algorithm functions. The second example with the array uses the same remove_if algorithm function. The remove_if returns the start of the "erased" data, as remove_if doesn't actually remove, but moves the data to the end of the sequence.

Sheldonshelduck answered 17/12, 2014 at 7:57 Comment(2)
with C++11, you may use std::end(Array) instead of Array + aSize (to avoid the error prone sizeof).Fechner
And to be symmetric you may also use std::begin().Fechner
Q
1

i am try swap concept without using 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();
}
Quadruped answered 17/12, 2014 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.