vector::erase function


There are two types of erase function in vector:

1 iterator erase(const_iterator pos );
2 iterator erase(const_iterator first , const_iterator last);

Both these functions remove data from the vector.After the data is removed from the vector the size of the vector will decrease,however the capacity of the vector remains the same.A more in-depth discussion of these two functions is provided below.



iterator erase(const_iterator pos );

This function can remove only one element from the vector at a time.If you want to remove a range of values from the vector then use the next function.

Parameters:
pos – A const_iterator type that denotes the position from which the data is to be removed.

Return type
iterator – It point to the position next to the position from where the data is removed.If the data removed is the last element then it point to end( ).

After calling this function the vector size is reduced by 1.But the capacity of the vector remains the same.

Code example:

vector< int > vec={ 89 , 45 , 68 } ;

cout<< vec.capacity() << endl ;

vec.erase( vec.begin() ) ; ///removes the first data

for(auto elem:vec)
{
cout<< elem << ” , ” ;
}

cout<< vec.size() ;
cout<< vec.capacity() << endl ;

Output

3
45 , 68
2
3

You can see that the capacity remains the same.

The code below verify that the iterator returned by this function points to the position next to the data removed.

vector< int > vec={ 89 , 45 , 68 };

vector< int > ::vecIt=vec.erase( vec.begin() ) ;

cout<< *vecIt ;

Output

68

Since 89 is removed the next data which is 45 becomes the first element and the value 68 is moved up to the second position.Hence vecIt points to the value 68.A pictorial representation might help you understand better.
vector erase






iterator erase(const_iterator first , const_iterator last );

Unlike the first function which remove only one element from the vector at a time ,this function removes a range of data from the vector.

Parameters:
first – A const_iterator type that denotes the starting position of the range to be removed.

second – A const_iterator type pointing to one past the last element of the range to be removed.

Return type
iterator – An iterator pointing to the position pointed by the ‘last’ argument prior to removing the data.If the ‘last’ argument points to end() then the returned iterator also point to end().

In this case also only the size of the vector is reduced,the capacity remains the same.

Code example:

vector<int> vecNew = { 34 , 56 , 89 , 123 } ;

vector<int>::iterator vecNewIt=vecNew.erase( vecNew.begin() , vecNew.begin()+2 ); //Removes the first two elements

for(auto elem:vecNew)
{
cout<< elem << ” , ” ;
}

///accessing the returned iterator
cout<< *vecNewIt ;

Output,

89 , 123
89


 


Stop wasting time,earn money($$$) from your website-Join Now!

Precautions to take while storing pointer in vector

If the vector hold a pointer that points to some other memory ,then these functions removes only the pointer not the memory pointed by the pointer.It is the user’s responsibility to handle the memory pointed by the pointer.An instance of such case is shown below.

int val=456 , *mem=&val ,
*i1=new int(12) , *i2=new int(91) , *i3=new int(389) ;

vector<int*> vecPtr={ i1 , i2 , i3 , mem } ;

///Accessing the data pointed by the pointers stored in the vector
for(auto elem:vecPtr)
{
cout<< *elem << ” , ” ;
}

vecPtr.erase( vecPtr.begin( ) , vecPtr.begin()+2 ) ; ///erasing the first two pointer elements from the vector

///accessing the data after the pointers are erased from the vector
cout<< *i1 << endl
<< *i2 << endl ;

///deleting the storage
delete i1 ; ///do not forget!
delete i2 ; ///do not forget!
delete i3 ; ///do not forget!

Output,

12 , 91 , 389 , 456

12
91

We can still access the data pointed by the pointers erased from the vector.So,we must not forget to delete the storage using the ‘delete operator‘ else there will be memory leakage.

A better way to handle dynamic storage is to use smart pointers instead of the traditional raw pointers.The smart pointers can deallocate storage without needing to call the delete operator for each storage explicitly by us.More about smart pointers can be found here shared_ptr – a smart pointer.