C++ std::string cbegin() Function

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

Definition

In C++, std::string::cbegin() returns a const_iterator to the first character.

Use it when you want a read-only walk even on a non-const string. Include <string>. (C++11)

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

Call cbegin for a read-only starting iterator.

C++
auto it = s.cbegin();
CallWhat it does
s.cbegin()const_iterator to the first character.

On a const string, begin() already returns a const_iterator. cbegin() makes the intent obvious on mutable strings.

Library declarations
C++
const_iterator cbegin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
const_iteratorRead-only iterator to the first character.

C++ Examples

Example 1 — Basic cbegin()

Read the first character.

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

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

How It Works

  1. cbegin() points at the first character.
  2. Dereferencing reads 'R'.
  3. The iterator is const, so it cannot assign.

Example 2 — Loop With cbegin/cend

Print all characters read-only.

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

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

How It Works

  1. The loop uses const iterators.
  2. Each character is printed.
  3. No modification is possible through it.

Example 3 — Algorithm With Const Iters

Use find with cbegin/cend.

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

int main() {
    std::string s = "banana";
    auto it = std::find(s.cbegin(), s.cend(), 'n');
    if (it != s.cend()) {
        std::cout << "index: " << (it - s.cbegin()) << '\n';
    }
    return 0;
}

How It Works

  1. std::find searches the half-open range.
  2. The first 'n' is at offset 2.
  3. Using cbegin/cend keeps the search read-only.

Frequently Asked Questions

Returns a const_iterator to the first character.
To force a read-only iterator even when the string is non-const.
C++11.
No.
cend(), and for reverse order crbegin()/crend().

Next: cend()

Keep going through the string functions. const_iterator one past the last character.

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

7 people found this page helpful