The difference between front () and begin ()

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

+7
source share
4 answers

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

+12
source

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

Note that you should not call front in an empty container, but it is normal to call begin if you do not find the iterator returned by begin .

+3
source

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

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

+1
source

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

0
source

All Articles