### erase element from vector

hey guys can help me erase the first element in my two vectors i tried using vector.erase(vector.begin()) but my code is crushing.....any help would be highly appreciated thanx in advance

 ``123456789101112131415161718192021222324252627282930313233343536373839`` `````` #include #include #include using namespace std; void convertkW(vectorkW,vectorhp) { for(unsigned int i=0; i<9; i++) { kW.push_back(0.05*i*i+0.15*i); hp.push_back(kW.at(i)*1.341); cout<kW,vectorhp) { cout<kW; vectorhp; convertkW(kW,hp); converthp(kW,hp); return 0; }``````
Where do you try to erase the first element?
BEFORE OUTPUTING THE RESULTS...DONT WANT THE FIRST ELEMENT TO APPEAR
Is your vector has atleast one element?

before calling 'erase', please check for non empty.

Try like this

if (!vector.empty())
vector.erase(vector.begin());
please show the code where the problem occurs
line 12 and 13 has the problem
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include #include #include using namespace std; void convertkW(vectorkW,vectorhp) { for(unsigned int i=0; i<9; i++) { kW.push_back(0.05*i*i+0.15*i); hp.push_back(kW.at(i)*1.341); kW.erase(kW.begin()+1); hp.erase(hp.begin()+1); cout<kW,vectorhp) { cout<kW; vectorhp; convertkW(kW,hp); converthp(kW,hp); return 0; } ``````
line 12/13 erase the second element not the first. Remove the +1. And do like NVTKrishna said: check for empty before erase

I'd say take lines 12/13 out of the for loop. This way you erase an element each time you add another.
Topic archived. No new replies allowed.