C++ string begin() Function
Example
Get the first character of a string with an iterator:
string txt = "Hello";
cout << *txt.begin();
Definition and Usage
The begin() function returns an iterator (an object that points to a character, similar to a pointer). It points to the first character of the string. Use * in front of the iterator to get the character.
begin() is often used together with end() to loop through the characters of a string.
The begin function is defined in the <string> header file.
Tip: Learn more about iterators in our Iterators chapter.
Syntax
string.begin();
Parameter Values
None.
Technical Details
| Returns: | An iterator pointing to the first character of the string. |
|---|
Related Pages
Read more about strings in our String Tutorial.