C++ list begin() Function
Example
Get the first element of a list with an iterator:
list<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
list<string>::iterator it = cars.begin();
cout << *it;
Definition and Usage
The begin() function returns an iterator (an object that points to an element, similar to a pointer). It points to the first element of the list. Use * to get the value of the element.
begin() is often used together with end() to loop through a list.
The begin function is defined in the <list> header file.
Tip: Learn more about iterators in our Iterators chapter.
Syntax
list.begin();
Parameter Values
None.
Technical Details
| Returns: | An iterator pointing to the first element of the list. |
|---|
Related Pages
Read more about lists in our List Tutorial.