Why do I get the front element value after I clear the vector? Am I missing some obvious thing? I tried using the erase function too and got the same result. What is the expected result here? size of vector shows 0 too after clearing but vect.front () shows a value. My code is as follows:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector <int> vect;
vector <int>::iterator it;
vect.push_back(10);
vect.push_back(3);
vect.push_back(5);
for( int i=0;i<vect.size();i++)
{
cout<<vect[i]<<endl;
}
vect.clear();
cout<<"front element "<<vect.front()<<endl;
cout<<"last element "<<vect.back()<<endl;
if(vect.empty())
cout<<"empty"<<endl;
return 0;
}
output is:
nm@nm:~$ g++ vectortest2.cpp
nm@nm:~$ ./a.out
10
3
5
front element 10
last element 0
empty
nm@nm:~$