C++ std::string begin() Function

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

Definition

In C++, std::string::begin() returns an iterator pointing at the first character.

If the string is empty, begin() equals 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

Use with end() in loops, or dereference when the string is not empty.

C++
auto it = s.begin();
for (auto it = s.begin(); it != s.end(); ++it) { }
CallWhat it does
s.begin()Iterator to the first character, or end() when empty.

Prefer a range-based for loop when you only need the characters: for (char c : s).

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

Parameters

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

Return Value

Return ValueDescription
iteratorMutable iterator on a non-const string; const_iterator on a const string.

C++ Examples

Example 1 — Basic begin()

Print the first character through an iterator.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Yes";
    std::cout << *s.begin() << '\n';
    return 0;
}

How It Works

  1. begin() points at the first character.
  2. Dereferencing it yields 'Y'.
  3. The string itself is unchanged.

Example 2 — Loop With begin()/end()

Print every character with iterators.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "ab";
    for (auto it = s.begin(); it != s.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop starts at begin().
  2. It stops when the iterator reaches end().
  3. Each step prints one character.

Example 3 — Write Through begin()

Uppercase the first character.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "cpp";
    *s.begin() = 'C';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. A non-const begin() iterator can write.
  2. Assigning 'C' changes the first character.
  3. The string becomes "Cpp".

Frequently Asked Questions

An iterator to the first character, or end() if the string is empty.
No. That is undefined behavior because begin() equals end().
When the string reallocates or you erase/replace in ways the standard says invalidate iterators.
Not exactly. Prefer iterators or data()/c_str() over taking addresses of s[0] on potentially empty strings.
Range-based for is simpler for reading. Use begin()/end() when you need the iterator itself.

Next: c_str()

Keep going through the string functions. const char* to a null-terminated C-string view of the text.

c_str() 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.

13 people found this page helpful