The difference between front() and begin()

微笑、不失礼 提交于 2020-01-01 07:38:20

问题


What is the difference between the front() and begin() functions that appears in many STL containers?


回答1:


begin() returns an iterator that can be used to iterate through the collection, while front() just returns a reference to the first element of the collection.




回答2:


front() returns a reference to the first element, begin() returns an iterator to it.

Note that you shouldn't call front on an empty container, but it's OK to call begin as long as you don't dereference the iterator that begin returns.




回答3:


The front member returns a reference to the first member of a list or vector. The begin function returns an iterator (which is more like a pointer) initialized to the first member of a list, map, or vector.




回答4:


From http://www.cplusplus.com/reference/stl/vector/begin/ (literally the first google result for "vector::begin"):

Notice that unlike member vector::front, which returns a reference to the first element, this function returns a random access iterator.



来源:https://stackoverflow.com/questions/9303110/the-difference-between-front-and-begin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!