Home C++ Strings Functions end() C++ std::string end() Function Definition In C++, std::string::end() returns an iterator that points one past the last character .
It marks where a forward loop should stop. Never read or write through end(). Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax Compare other iterators with end(); do not dereference it.
if (s.begin() == s.end()) { /* empty */ }
for (auto it = s.begin(); it != s.end(); ++it) { } Call What it does s.end()Past-the-end iterator.
On an empty string, begin() == end(). That is the normal empty state.
Library declarations iterator end() noexcept;
const_iterator end() const noexcept; Parameters Parameter Description None end() takes no parameters. Call it with empty brackets.
Return Value Return Value Description iterator Past-the-end iterator.
C++ Examples 1. Detect empty 2. Stop a loop 3. Count with distance Example 1 — begin Equals end Check emptiness with iterators.
#include <iostream>
#include <string>
int main() {
std::string a;
std::string b = "x";
std::cout << std::boolalpha;
std::cout << (a.begin() == a.end()) << '\n';
std::cout << (b.begin() == b.end()) << '\n';
return 0;
} How It Works Empty string: begin and end compare equal. Non-empty string: they differ. empty() is still the clearer check in most code.Example 2 — Loop Until end() Iterate all characters.
#include <iostream>
#include <string>
int main() {
std::string s = "OK";
for (auto it = s.begin(); it != s.end(); ++it) {
std::cout << *it;
}
std::cout << '\n';
return 0;
} How It Works The loop condition uses != s.end(). Each step dereferences a valid iterator. When it reaches end, the loop stops. Example 3 — Distance to end() Measure length via iterator distance.
#include <iostream>
#include <iterator>
#include <string>
int main() {
std::string s = "four";
auto n = std::distance(s.begin(), s.end());
std::cout << n << '\n';
return 0;
} How It Works distance counts steps from begin to end.For string iterators this equals size(). Prefer size() when you only need the count. Frequently Asked Questions What is end()? An iterator one past the last character. It is a stop marker, not a character.
Can I dereference end()? No. That is undefined behavior.
What does begin() == end() mean? The string is empty.
Does end() change after append? A reallocation invalidates iterators. After growth you must call end() again.
How do I get the last character with end()? Use *(s.end() - 1) only on a non-empty string, or simply call back().
Next: ends_with() Keep going through the string functions. Test whether the string ends with a suffix (C++20).
ends_with() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion