C++ at() function
Among all ways to access array elements,at is the safest one, because it providesbounds checking.
at is a member function of the container class, used toreturn the element at the specified position, while performing bounds checking. If the index is out of range, it throws std::out_of_range exception.
at implements safe element access, allowing you to catch and handle out-of-bounds errors at runtime.
Word meaning: at means 'at... position', i.e., to get the element at the specified position.
Basic syntax and parameters
at is a member function of the container class, so you need to have a container object first, then use the dot operator . to call it.
Syntax format
reference at(size_type pos);
const_reference at(size_type pos) const;
Parameter Description
- Parameters:
pos - Type:
size_type(unsigned integer type, usuallysize_t) - Description: The position (index) of the element to access. Indexing starts at 0, and the maximum valid index is
size() - 1.
Function description
- Return Value: Returns areferenceto the element at the specified position. If the container is const, a const reference is returned.
- effect: Returns the element at the specified position. If the index is out of the valid range (
pos >= size()), it throwsstd::out_of_rangeexception. - Difference from operator[]:
atperforms bounds checking,operator[]does not perform bounds checking (out-of-bounds behavior is undefined).
Example
Let us thoroughly master at through a series of examples from simple to complex.
Example 1: Basic usage - accessing elements
Example
#include
#include
#include
int
main
(
)
{
// 1. Create and initialize an array
std
::
array
<
int
,
5
numbers
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"array size is: "
<<
numbers.
size
(
)
<<
std
::
endl
;
// 2. Use at to access elements
std
::
cout
<<
"First element (at(0)): "
<<
numbers.
at
(
0
)
<<
std
::
endl
;
std
::
cout
<<
"Second element (at(1)): "
<<
numbers.
at
(
1
)
<<
std
::
endl
;
std
::
cout
<<
"Third element (at(2)): "
<<
numbers.
at
(
2
)
<<
std
::
endl
;
std
::
cout
<<
"Last element (at(4)): "
<<
numbers.
at
(
4
)
<<
std
::
endl
;
// 3. Use a loop to access all elements
std
::
cout
<<
"All elements: "
;
for
(
size_t
i
=
0
;
i
<
numbers.
size
(
)
;
++
i
)
{
std
::
cout
<<
numbers.
at
(
i
)
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
array的大小是: 5
第一个元素 (at(0)): 10
第二个元素 (at(1)): 20
第三个元素 (at(2)): 30
最后一个元素 (at(4)): 50
所有元素: 10 20 30 40 50
Code analysis:
numbers.at(0)returns the first element10(indexing starts at 0).numbers.at(4)returns the last element50(becausesize()is 5, and the valid indices are 0-4).- Using a loop and
atcan safely traverse all elements.
Example 2: Catching out-of-bounds exceptions
at The main advantage of is that it can throw exceptions, allowing us to handle out-of-bounds errors.
Example
#include
#include
#include
int
main
(
)
{
std
::
array
<
int
,
3
numbers
=
{
10
,
20
,
30
};
// Try to access an out-of-bounds position
try
{
std
::
cout
<<
"Trying to access at(10)..."
<<
std
::
endl
;
int
value
=
numbers.
at
(
10
)
;
// This will throw an exception
std
::
cout
<<
"Value: "
<<
value
<<
std
::
endl
;
}
catch
(
const
std
::
out_of_range
&
e
)
{
std
::
cout
<<
"Caught exception: "
<<
e.
what
(
)
<<
std
::
endl
;
}
// Normal access
try
{
std
::
cout
<<
"Accessing at(2): "
<<
numbers.
at
(
2
)
<<
std
::
endl
;
}
catch
(
const
std
::
out_of_range
&
e
)
{
std
::
cout
<<
"Caught exception: "
<<
e.
what
(
)
<<
std
::
endl
;
}
return
0
;
}
Expected output:
尝试访问 at(10)...
捕获到异常: array::at: __pos (which is 10) >= _Nm (which is 3)
访问 at(2): 30
Code analysis:
- When accessing
at(10), because the index is out of range, anstd::out_of_rangeexception. catch(const std::out_of_range& e)exception is thrown. The catch blocke.what()returns the exception's description.- Normal index access does not throw exceptions.
Example 3: Modifying element values
at returns a reference, so it can be used to modify the element's value.
Example
#include
#include
int
main
(
)
{
std
::
array
<
int
,
3
numbers
=
{
10
,
20
,
30
};
std
::
cout
<<
"Before modification: "
;
for
(
size_t
i
=
0
;
i
<
numbers.
size
(
)
;
++
i
)
{
std
::
cout
<<
numbers.
at
(
i
)
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Use at() to get a reference and modify the element
numbers.
at
(
0
)
=
100
;
numbers.
at
(
1
)
=
200
;
numbers.
at
(
2
)
=
300
;
std
::
cout
<<
"After modification: "
;
for
(
size_t
i
=
0
;
i
<
numbers.
size
(
)
;
++
i
)
{
std
::
cout
<<
numbers.
at
(
i
)
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
修改前: 10 20 30
修改后: 100 200 300
Code analysis:
numbers.at(0) = 100;modified the value of the first element through the reference.- This demonstrates that
at()returns a modifiable lvalue reference.
The C++ container class