In the below code if I try to remove any element except last one the code works fine . But if I try to delete last element it throws run time error . Not sure why ?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> s;
s.push_back("Jacob");
s.push_back("Jamal");
s.push_back("Joseph");
s.push_back("Janardan");
vector<string>::iterator it;
for(it = s.begin(); it != s.end() ; it++)
cout<<*it<<endl;
for(it = s.begin(); it != s.end() ; it++)
if(*it == "Janardan")
s.erase(it);
for(it = s.begin(); it != s.end() ; it++)
cout<<*it<<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire