c++stl

What do I get from front() of empty std container?


If front() returns a reference and the container is empty what do I get, an undefined reference? Does it mean I need to check empty() before each front()?


Solution

  • You get undefined behaviour - you need to check that the container contains something using empty() (which checks if the container is empty) before calling front().