std::basic_string<CharT,Traits,Allocator>::at
cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| (1) | ||
reference at( size_type pos ); |
( C++20) | |
constexpr reference at( size_type pos ); |
( C++20) | |
| (2) | ||
const_reference at( size_type pos ) const; |
( C++20) | |
constexpr const_reference at( size_type pos ) const; |
( C++20) | |
pos. , std::out_of_range.
| pos | , |
.
std::out_of_range, pos >= size().
.
#include <iostream>
#include <stdexcept>
#include <string>
int main()
{
std::string s("message"); //
s = "abc";
s.at(2) = 'x'; // OK
std::cout << s << '\n';
std::cout << " = " << s.size() << '\n';
std::cout << " = " << s.capacity() << '\n';
try
{
// ,
// .
s.at(3) = 'x';
}
catch (std::out_of_range const& exc)
{
std::cout << exc.what() << '\n';
}
}
:
abx
= 3
= 7
basic_string::at
C++:
| LWG 847 | C++98 |
| (public -) | |
(C++17) |
(public - std::basic_string_view<CharT,Traits>)
|