问题
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