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

前端 未结 5 850
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 12:50

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

5条回答
  •  情歌与酒
    2021-02-01 13:11

    Yes, you can use 'at' like Graham mentioned instead of using front.

    But, at(0) is only available for some containers - vectors, deque and not for others - list, queue, stack. In these cases you've to fall back on the safety of the 'empty' check.

提交回复
热议问题