Array removal

If I have an array of fixed size of say
const int max_size = 200;
and I want to remove one of the elements, how would I do so?
I know normally you would move the undesired value to the last element of the array then decrement the size of the array, but I don't think I can do that in this case?

thanks
Arrays cannot be resized, that's their whole point. Use a vector or another C++ container.
You have to keep the actual size of the array along with its maximum size.

for example

1
2
3
4
5
6
7
8
9
10
11
const int N = 10;
int a[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int current_size = N;

current_size = std::distance( std::begin( a ), 
                              std::remove_if( std::begin( a ), std::end( a ),
                                              []( int x ) { return ( x % 2 ); } ) );

std::copy( std::begin( a ), std::next( std::begin( a ), current_size ),
                std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
Last edited on
Topic archived. No new replies allowed.