I am trying to understand the behavior of vector::pop_back(). So I have following code snippet:
vector<int> test;
test.push_back(1);
test.pop_back();
cout << test.front() << endl;
Maybe it is right but it surprises me that it prints out 1. So I am confused. Is pop_back() only able to remove the element has index > 0 ?
Thanks in advance!
You are invoking undefined behaviour by calling
fronton an empty vector. That’s like indexing out of the bounds of an array. Anything could happen, including returning1.