C++ .at()

MamtaWardhani's avatar
Published Jul 2, 2025
Contribute to Docs

The .at() method in C++ is a member function of the std::array container that provides safe access to elements by their position index. Unlike the subscript operator ([]), the .at() method automatically performs bounds checking and throws an std::out_of_range exception if the specified index is invalid. This makes it a safer alternative for element access when you need to ensure that array bounds are not violated.

The .at() method is particularly useful in scenarios where data integrity and error handling are critical, such as processing user input, parsing data files, or working with indices that might be calculated dynamically. It provides a balance between safety and performance, making code more robust while maintaining reasonable execution speed.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

array_name.at(i)

Parameters:

  • i: The zero-based index position of the element to access. Must be a non-negative integer value of type size_type (typically std::size_t). Valid indices range from 0 to size()-1. If the index is greater than or equal to the array size, an std::out_of_range exception is thrown.

Return value:

The .at() method returns a reference to the element at the specified position. If the array object is const-qualified, it returns a const_reference; otherwise, it returns a reference that allows both reading and modifying the element.

Exception:

Throws std::out_of_range exception if pos is greater than or equal to the size of the array.

Example 1: Basic Element Access

This example demonstrates the fundamental usage of the .at() method to safely access elements in a std::array:

#include <iostream>
#include <array>
int main() {
// Create an array of integers with 5 elements
std::array<int, 5> numbers = {10, 20, 30, 40, 50};
// Access elements using .at() method
std::cout << "Element at index 0: " << numbers.at(0) << std::endl;
std::cout << "Element at index 2: " << numbers.at(2) << std::endl;
std::cout << "Element at index 4: " << numbers.at(4) << std::endl;
// Modify an element using .at() method
numbers.at(1) = 99;
std::cout << "Modified element at index 1: " << numbers.at(1) << std::endl;
return 0;
}

The output of this code is:

Element at index 0: 10
Element at index 2: 30
Element at index 4: 50
Modified element at index 1: 99

This example shows how .at() can be used for both reading and writing array elements. The method provides direct access to elements at valid indices and allows modification of non-const arrays.

Example 2: Safe Input Processing

This example demonstrates using .at() method in a real-world scenario where user input needs to be validated before accessing array elements:

#include <iostream>
#include <array>
#include <stdexcept>
int main() {
// Array storing daily temperatures for a week
std::array<double, 7> weeklyTemps = {22.5, 25.1, 21.8, 24.3, 26.7, 23.9, 20.2};
std::array<std::string, 7> dayNames = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
int dayChoice;
std::cout << "Enter day number (1-7) to get temperature: ";
std::cin >> dayChoice;
try {
// Convert 1-based input to 0-based index and use .at() for safe access
int index = dayChoice - 1;
double temperature = weeklyTemps.at(index);
std::string dayName = dayNames.at(index);
std::cout << "Temperature on " << dayName << ": " << temperature << "°C" << std::endl;
}
catch (const std::out_of_range& e) {
std::cout << "Error: Invalid day number. Please enter a number between 1 and 7." << std::endl;
}
return 0;
}

The output of this code is (If user enters 3):

Enter day number (1-7) to get temperature: 3
Temperature on Wednesday: 21.8°C

Output (if user enters 10):

Enter day number (1-7) to get temperature: 10
Error: Invalid day number. Please enter a number between 1 and 7.

This example demonstrates how .at() method’s bounds checking helps create robust applications that handle invalid user input gracefully without crashing.

Codebyte Example: Grade Management System

This example shows the .at() method being used in a practical grade management scenario where safe access to student scores is essential:

#include <iostream>
#include <array>
#include <iomanip>
#include <stdexcept>
int main() {
// Array storing test scores for 5 students
std::array<double, 5> testScores = {85.5, 92.3, 78.9, 96.1, 88.7};
std::array<std::string, 5> studentNames = {"Alice", "Bob", "Charlie", "Diana", "Eve"};
// Function to display a specific student's grade
auto displayGrade = [&](int studentId) {
try {
double score = testScores.at(studentId);
std::string name = studentNames.at(studentId);
std::cout << std::fixed << std::setprecision(1);
std::cout << "Student: " << name << ", Score: " << score << "%" << std::endl;
// Determine grade based on score
if (score >= 90) std::cout << "Grade: A" << std::endl;
else if (score >= 80) std::cout << "Grade: B" << std::endl;
else if (score >= 70) std::cout << "Grade: C" << std::endl;
else std::cout << "Grade: F" << std::endl;
}
catch (const std::out_of_range& e) {
std::cout << "Error: Student ID " << studentId << " not found." << std::endl;
}
};
// Display all students
std::cout << "=== Class Grades ===" << std::endl;
for (size_t i = 0; i < testScores.size(); ++i) {
displayGrade(i);
std::cout << std::endl;
}
// Test invalid access
std::cout << "Attempting to access invalid student ID:" << std::endl;
displayGrade(10);
return 0;
}

This example illustrates how the .at() method enables the creation of robust data management systems where array bounds are automatically validated, preventing potential crashes and providing clear error feedback.

Frequently Asked Questions

1. What is the difference between .at() and the subscript operator []?

The .at() method performs bounds checking and throws an std::out_of_range exception for invalid indices, while the subscript operator [] does not check bounds and may result in undefined behavior if an invalid index is used.

2. When should I use .at() instead of []?

Use .at() when you need guaranteed bounds checking, especially when dealing with user input, dynamic indices, or when building robust applications where safety is more important than maximum performance.

3. What happens if I try to access an element beyond the array size?

The .at() method will throw an std::out_of_range exception, which you can catch and handle appropriately in your code.

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours