C++ std::string at() Function

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

Definition

In C++, std::string::at() gives you a reference to one character by its index. Counting starts at 0.

at() is the safe indexed access. If the index is out of range, it throws std::out_of_range. The unchecked s[i] operator does not throw. 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

Pass a zero-based index that must be less than size().

C++
char ch = s.at(0);
s.at(1) = 'X';
CallWhat it does
s.at(i)Returns a reference to the character at index i. Throws if i >= s.size().
cs.at(i)On a const string, returns a const reference (read-only).

Prefer at() when the index might be wrong and you want a clear exception. Use s[i] in hot loops when you already know the index is valid.

Library declarations
C++
char& at(size_type pos);
const char& at(size_type pos) const;

Parameters

ParameterDescription
posZero-based index. Must satisfy pos < size().

Return Value

Return ValueDescription
char&Reference to the character. For a const string, const char&.

C++ Examples

Example 1 — Basic at()

Read characters by index.

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

int main() {
    std::string s = "Cat";
    std::cout << s.at(0) << '\n';
    std::cout << s.at(2) << '\n';
    return 0;
}

How It Works

  1. Index 0 is the first character 'C'.
  2. Index 2 is the last character 't'.
  3. Both calls succeed because the indices are inside the string.

Example 2 — Assign Through at()

Replace one character using the returned reference.

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

int main() {
    std::string s = "bat";
    s.at(0) = 'c';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. at(0) returns a reference to the first character.
  2. Assigning 'c' changes that character in place.
  3. The string becomes "cat".

Example 3 — at() Throws

Catch std::out_of_range for a bad index.

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

int main() {
    std::string s = "hi";
    try {
        std::cout << s.at(5) << '\n';
    } catch (const std::out_of_range&) {
        std::cout << "index too big\n";
    }
    return 0;
}

How It Works

  1. The string only has indices 0 and 1.
  2. at(5) is out of range, so it throws.
  3. The catch block prints a safe message.

Frequently Asked Questions

It returns a reference to the character at a given index, after checking that the index is valid.
at() throws std::out_of_range.
at() checks bounds and throws. operator[] does not check; a bad index is undefined behavior.
Only if you somehow had a valid index, which you do not—size is 0—so at(0) throws.
Yes. On a const string it returns a const reference so you can read but not assign.

Next: back()

Keep going through the string functions. Reference to the last character. Undefined on an empty string.

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

11 people found this page helpful