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