Please tell me how to delete an element from a C++ array.
My teacher is setting its value to 0, is that correct?
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;