2

I have a one-dimensional array of size 10. I want to remove the elements from 5 to 8. Can somebody give me an example of how to do it? This is how I defined my array but I have no idea about how to start.

#include <iostream>
#include <iomanip> 
using namespace std;

int array[10] = {1,2,3,4,5,6,7,8,9,10};

So, the output should be 1,2,3,4,5,10. (index 0 = element 1)

Thanks

user2147241
  • 53
  • 1
  • 9
  • 1
    possible duplicate of [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – 2501 Nov 10 '14 at 03:22
  • If you want dynamically sizable arrays use `std::vector`. – Captain Obvlious Nov 10 '14 at 03:23
  • `array`'s size can't be changed...so you can't "remove" anything in your array, you maybe can mark invalid elements as `-1` or something and put them in the back of your `array`. – Marson Mao Nov 10 '14 at 03:26
  • Use `std::vector` and `std::vector::erase`. – n. m. could be an AI Nov 10 '14 at 03:34
  • You can still use `std::remove_if` with the array (or `std::array`) if needed. It returns an iterator to the new end. I'm not sure if the array's always going to be sorted or the indices always known, so it might come in handy. – chris Nov 10 '14 at 03:37

4 Answers4

1

An alternative to arrays is to use a vector. In this case you can do:

#include <vector>

// create vector for integers
std::vector<int> v;

// set values from 1 to 10
for (int i=1; i<=10; i++) 
    v.push_back(i);

// erase from 5 to 8
v.erase (v.begin()+5, v.begin()+9);

BTW, if your compiler supports C++11, you can initialize your vector as:

std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
JosEduSol
  • 5,268
  • 3
  • 23
  • 31
0

You can't literally remove them because C++'s built-in arrays (like C's) are fixed-size.

What you can do is overwrite them with the values that come after them, and then (if you want to) zero out the four now-extra positions at the end of the array. e.g.:

// copy latter values over the values we don't want
for (int i=4; i<6; i++) array[i] = array[i+4];

// zero out the no-longer-necessary values at the end, just so they won't appear valid anymore
for (int i=6; i<10; i++) array[i] = 0;

and then when printing the array, print only the first 6 values rather than all 10, since your array is (conceptually, even if not actually) "shorter" now.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

In the above case, its an array of 10 elements array[10]. As the array size is fixed, it may not be possible to remove an item from the array. Rather, you can assign null or someother value to the corresponding element.

BTW, I would rather use, std::list or std:: vector instead of array.

Anyhow, If you mean to get rid of that element, you'd need to shift all the ones to the right of that element one to the left:

for (int i = index; i < /* length */; i++)
array[index] = array[index+1];
array[length-1] = 0;
user 12321
  • 2,846
  • 1
  • 24
  • 34
0

Not sure what exactly you want your output to be, so I'm just providing another possible solution here:

    // your input
    int array[10] = {1,2,3,4,5,6,7,8,9,10};

    // 1. put all invalid element in the back of the array
    //    "it" points to the first invalid element
    int* it = std::remove_if(std::begin(array), std::end(array), 
    [](int e)
    { 
        return (e >= 5) && (e <= 8);
    });

    // 2. then, mark them as -1 (from "it" to the last element)
    std::transform(it, std::end(array), it,
    [](int e) -> int
    {
        return -1;
    });

    // now the array contains:
    // {1,2,3,4,9,10,-1,-1,-1,-1}
Marson Mao
  • 2,935
  • 6
  • 30
  • 45