
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Array::at() Function
The C++ std::array::at() function provides a way to access elements in a array with bounds checking. It returns a reference to the element at a specified position within the array.
Unlike the operator[], which does not perform bounds checking, at() throws an out_of_range exception if the provided index is out of the valid range (0 to size-1).
Syntax
Following is the syntax for std::array::at() function.
reference at ( size_type n ); const_reference at ( size_type n ) const;
Parameters
- N − It indicates the position of an element in the array.
Return Value
This function returns the element at the specified position in the array.
Exceptions
This function throws out_of_range expception if value of N is not valid array index.
Time complexity
Constant i.e. O(1)
Example 1
In below example step-1 prints array contents without exception. Step-2 shows exception handling using try-catch block.
#include <iostream> #include <array> #include <stdexcept> using namespace std; int main(void) { array < int, 5 > arr = {10, 20, 30, 40, 50}; size_t i; for (i = 0; i < 5; ++i) cout << arr.at(i) << " "; cout << endl; try { arr.at(10); } catch (out_of_range e) { cout << "out_of_range expcepiton caught for " << e.what() << endl; } return 0; }
Output
Output of the above code is as follows −
10 20 30 40 50 out_of_range expcepiton caught for array::at: __n (which is 10) >= _Nm (which is 5)
Example 2
Consider the following example, where we are going to get the index location of the integer using the at() function.
#include <iostream> #include <array> using namespace std; int main() { array < int, 10 > arr = {9, 12, 15, 18, 21, 24, 27, 30, 33, 36}; cout << "The location of the element at index 6 = " << arr.at(6) << endl; return 0; }
Output
Following is the output of the above code −
The location of the element at index 6 = 27
Example 3
Following is the example, where we are going to find thr array length is lesser than the index location using at() function.
#include <iostream> #include <array> using namespace std; int main() { array < int, 10 > arr = {9, 12, 15, 18, 21, 24, 27, 30, 33, 36}; cout << "The location of the element at index 20 = " << arr.at(20) << endl; return 0; }
Output
If we run the above code it will generate the following output −
terminate called after throwing an instance of 'std::out_of_range' what(): array::at: __n (which is 20) >= _Nm (which is 10) Aborted