C++ list end() Function
Example
Loop through a list with begin() and end():
list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
list<string>::iterator it;
for (it = cars.begin(); it != cars.end(); ++it) {
cout << *it << "\n";
}
Definition and Usage
The end() function returns an iterator (an object that points to an element, similar to a pointer). It points to the position after the last element of the list.
end() does not point to a real element, so do not use * on it. It is used to know where the list ends, for example to stop a loop.
The end function is defined in the <list> header file.
Tip: To get the last element, use back().
Syntax
list.end();
Parameter Values
None.
Technical Details
| Returns: | An iterator pointing to the position after the last element of the list. |
|---|
Related Pages
Read more about lists in our List Tutorial.