C++ set begin() Function
Example
Get the first element of a set:
set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};
auto it = cars.begin();
cout << *it;
Definition and Usage
The begin() function returns an iterator (a pointer-like object that points to an element) to the first element of a set.
The elements in a set are sorted, so the first element is the smallest value. For strings, this is the first one in alphabetical order. Use *it to read the value. You cannot change the value through the iterator, because that could break the sorted order.
The begin function is defined in the <set> header file.
Tip: The auto keyword lets the compiler find the type of the iterator for you. Learn more in the Iterators chapter.
Syntax
set.begin()
Parameter Values
None.
Technical Details
| Returns: | An iterator pointing to the first element of the set. If the set is empty, it is equal to set.end(). |
|---|
Related Pages
Read more about sets in our Sets Tutorial.