C++ vector end() function
Example
Get the last element in a vector:
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
vector<string>::iterator it = cars.end() - 1;
cout << *it;
Try it Yourself »
Definition and Usage
The end()
function returns an iterator pointing to the end of a vector.
Note: In C++, the "end" of a vector is actually one position after the last element. To access the last element, use vector.end() - 1
.
Syntax
vector.end();
Parameter Values
None.
Technical Details
Returns: | An iterator pointing to the end of the vector. |
---|
Related Pages
Read more about vectors in our Vector Tutorial.
Read more about iterators in our Iterators Tutorial.