Home C++ Strings Functions begin() C++ std::string begin() Function 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.
auto it = s.begin();
for (auto it = s.begin(); it != s.end(); ++it) { } Call What 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 iterator begin() noexcept;
const_iterator begin() const noexcept; Parameters Parameter Description None begin() takes no parameters. Call it with empty brackets.
Return Value Return Value Description iterator Mutable iterator on a non-const string; const_iterator on a const string.
C++ Examples 1. First char 2. Forward loop 3. Change via iterator Example 1 — Basic begin() Print the first character through an iterator.
#include <iostream>
#include <string>
int main() {
std::string s = "Yes";
std::cout << *s.begin() << '\n';
return 0;
} How It Works begin() points at the first character.Dereferencing it yields 'Y'. The string itself is unchanged. Example 2 — Loop With begin()/end() Print every character with iterators.
#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 The loop starts at begin(). It stops when the iterator reaches end(). Each step prints one character. Example 3 — Write Through begin() Uppercase the first character.
#include <iostream>
#include <string>
int main() {
std::string s = "cpp";
*s.begin() = 'C';
std::cout << s << '\n';
return 0;
} How It Works A non-const begin() iterator can write. Assigning 'C' changes the first character. The string becomes "Cpp". Frequently Asked Questions What does begin() return? An iterator to the first character, or end() if the string is empty.
Can I dereference begin() on an empty string? No. That is undefined behavior because begin() equals end().
When are begin iterators invalidated? When the string reallocates or you erase/replace in ways the standard says invalidate iterators.
Is begin() the same as &s[0]? Not exactly. Prefer iterators or data()/c_str() over taking addresses of s[0] on potentially empty strings.
Should I use begin() or a range-based for? 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.
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.
13 people found this page helpful
Helpful Share Copy link Suggestion