C++ std::string end() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

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.

C++
if (s.begin() == s.end()) { /* empty */ }
for (auto it = s.begin(); it != s.end(); ++it) { }
CallWhat it does
s.end()Past-the-end iterator.

On an empty string, begin() == end(). That is the normal empty state.

Library declarations
C++
iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

ParameterDescription
Noneend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
iteratorPast-the-end iterator.

C++ Examples

Example 1 — begin Equals end

Check emptiness with iterators.

C++
#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

  1. Empty string: begin and end compare equal.
  2. Non-empty string: they differ.
  3. empty() is still the clearer check in most code.

Example 2 — Loop Until end()

Iterate all characters.

C++
#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

  1. The loop condition uses != s.end().
  2. Each step dereferences a valid iterator.
  3. When it reaches end, the loop stops.

Example 3 — Distance to end()

Measure length via iterator distance.

C++
#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

  1. distance counts steps from begin to end.
  2. For string iterators this equals size().
  3. Prefer size() when you only need the count.

Frequently Asked Questions

An iterator one past the last character. It is a stop marker, not a character.
No. That is undefined behavior.
The string is empty.
A reallocation invalidates iterators. After growth you must call end() again.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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