tags:

views:

140

answers:

6

Please tell me how to delete an element from a c++ array ? my teacher is setting its value to 0, is it correct ?

+2  A: 

If you're talking about a normal array. e.g.

int array[100];

then you can't "delete" an eleement, since the array always has 100 elements (in this example).

So it depends on the interpretation your program makes of the array values. If your teacher is consistently using a value of 0 to mean non-existent element, everything will work and so that's as correct as any other approach.

Paul
A: 

Setting it to zero will still make that zero appear when you iterate over the array, or when you access it by index. If you do not want that, you have to copy all elements after the one you deleted one step towards the beginning of the array.

Space_C0wb0y
+4  A: 

You can't really "delete" an element from a C++ array.

However, if the array consists of pointers, you can delete the object that a specific element points to.

In your teacher's case, it will be important to make note of whether the objects of the array are dynamically allocated (using the new operator in C++) or not. He may simply be setting his values to 0 as an indicator that the value is no longer valid.

Without actual source code, this is as much as I can help you with.

KevenK
A: 

1) If you have an array of pointers, then like this :

// to create an array :
std::vector< int* > arr( 10, NULL );
for ( std::vector< int* >:iterator it=arr.begin; arr.end() != it; ++ it )
{
  *it = new int( 20 );
}
// to delete one element
delete( arr.at(3) );

Any access to this array element is formally an undefined behaviour by the c++ standard. Even assignment of NULL, like this:

arr.at(3) = NULL;

2) If you really must use pointers, then use smart pointers (this specific case requires shared_ptr) :

std::vector< std::shared_ptr< int > > arr;
VJo
+1  A: 

You can remove an element from a vector such that the element is no longer there and all the other elements shift a position.

struct counter
{
   int x;
   int operator()() { return x++; }
   counter() : x(0) {}
};

std::vector<int> v;
std::generate_n( std::back_inserter(v), 8, counter() );
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(cout, " "));
std::cout << '\n';
v.erase( v.begin() + 4 );

std::copy(v.begin(), v.end(), std::ostream_iterator<int>(cout, " "));
std::cout << '\n';

Should output:

0 1 2 3 4 5 6 7

0 1 2 3 5 6 7

(assume all necessary headers included and main function body etc).

Note that if you have a vector of pointers which were allocated with new, you would have to possibly call delete on the pointer before it was erased from the vector. (This depends on whether the vector manages the lifetime of these pointers). If you have a vector of boost::shared_ptr you will not need to manage the deletion.

CashCow
A: 

If the array is not intended to be sorted, a quick and easy way is to copy the last element to the position of the element to be deleted, then reduce the element count by 1.

PigBen