Here's an illustration of which is which
v: [ 1 | 2 | 3 | 4 | ... | 999 ]
front() back() end()
begin()
where front()
and back()
return a (const) reference to the first and last element respectively, and end()
returns an iterator (sort of a pointer) to one beyond the last element of vector. begin()
returns an iterator to the first element of a vector.
These are also explained at std::vector
front
access the first element
back
access the last element
end
/cend
returns an iterator to the end
begin
/cbegin
returns an iterator to the beginning
Typically, you start counting at one. This is different in C or C++,
where an index into an array or a sequence starts at zero. This is
the reason, why you must subtract one from size. This means,
in order to access the first element of an array, or in this case of a vector, you say
v[0]
and not
v[1]
Equally for the last (nth) element, you wouldn't take size
or n
of an array (a vector), but rather one less, e.g.
v[size() - 1]
or
v[n - 1]