I have a vector, which contains a class object.
I declared like this
vector<Aircraft> queue;
I erase the first element in the vector. But it seems like the first element still in the vector. I did this.
queue.erase(queue->begin());
cout<<queue.size(); //printed 0
Aircraft temp = queue.front();
cout<<temp.id; //expected a segfault error
The size of the vector is 0 after I erased the first element (there was only one element).
I was expecting a seg fault error when I tried to see the id of temp (Aircraft) since I erased the first element from the vector and the size is zero after that.
I even cleared the vector but ‘queue.front‘ still returns the object and cout statement still prints the id of the object.
It’s undefined behavior to access the
front()of avectorthat is empty so you must not do it and you can’t infer anything from the apparent behavior of your program if you do do it.The fact that
queue.size()is one less than before the.erasecall should tell you thaterasehas worked.