std::basic_string<CharT,Traits,Allocator>::at - cppreference.com
提供: cppreference.com
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
|
|
(C++20未満) | |
|
|
(C++20以上) | |
|
|
(C++20未満) | |
|
|
(C++20以上) | |
指定された位置 pos の文字を指す参照を返します。 範囲チェックが行われ、無効なアクセスの場合 std::out_of_range 型の例外が投げられます。
引数
戻り値
要求された文字を指す参照。
例外
pos >= size() の場合 std::out_of_range を投げます。
計算量
一定。
例
#include <stdexcept> #include <iostream> #include <string> int main() { std::string s("message"); // for capacity s = "abc"; s.at(2) = 'x'; // ok std::cout << s << '\n'; std::cout << "string size = " << s.size() << '\n'; std::cout << "string capacity = " << s.capacity() << '\n'; try { // throw, even if capacity allowed to access element s.at(3) = 'x'; } catch (std::out_of_range const& exc) { std::cout << exc.what() << '\n'; } }
出力例:
abx string size = 3 string capacity = 7 basic_string::at