Is the following code guaranteed by the standard to work(assuming st is not empty)?
#include
#include
int main()
{
extern std::st
I don't have a reference to the standard to back this up unfortunately, but there aren't many ways in which it could go wrong I guess:
std::vector
as the container type means that the elements must be stored in a std::vector
.st.top()
must return a reference to an element in the underlying container (i.e. an element in the std::vector
. Since the requirements on the container are that it supports back()
, push_back()
and pop_back()
, we can reasonably assume that top()
returns a reference to the last element in the vector.end
therefore points to one past the last element.start
therefore points to the beginning.Conclusion: Unless the assumption was wrong, it must work.
EDIT: And given the other answer's reference to the standard, the assumption is correct, so it works.