Erase function of vectors

please explain how vector.erase works, and how to set parameters in it

Hi @Learning_bunny
clear() function is used to remove all the elements of the vector container, thus making it size 0.

Syntax :
vectorname.clear()

Parameters :
No parameters are passed.

Result :
All the elements of the vector are removed ( or destroyed )

i want to know about erase not clear

one more thing if we resize a vector, will it delete the rest of the elements for eg if we have a vector has 10 elements i.e. its size is 10 but if we resize it to 5 then will it loose its last 5 elements or throw an error

erase() function removes from the vector either a single element (position) or a range of elements (first,last).

// erase the 6th element
myvector.erase (myvector.begin()+5);

// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);

Resize() function :
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

why do we always have to give myvector.begin as parameter in erase, and what if we want to erase some elemets in between like from 3rd to 7th element and why cant we do it with normal variables of by directly putting some numeric values

Because in erase parameters can only be vector iterator. Normal variable can not be used as argument for erase. If you want to delete from elements 3 to 7 then you have to do the following :

vector :: iterator it=v.begin();
v.erase((it+2),(it+7));

it here is a keyword or we can use any name here

You can use any name in place of it

get it now thankyou sir