名前空間
変種
操作

「cpp/string/basic string/at」の版間の差分

提供: cppreference.com
< cpp‎ | string‎ | basic string
(P0980R1)
 
2行: 2行:
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcl
 +
|
 
reference      at( size_type pos );
 
reference      at( size_type pos );
 +
 +
 
}}
 
}}
{{dcl |  
+
{{dcl
 +
|
 
const_reference at( size_type pos ) const;
 
const_reference at( size_type pos ) const;
 +
 +
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
27行: 33行:
  
 
===例===
 
===例===
{{
+
{{example
example
+
 
| code=
 
| code=
 
#include <stdexcept>
 
#include <stdexcept>
53行: 58行:
 
     }
 
     }
 
}
 
}
 +
 
| output=
 
| output=
 
abx
 
abx
66行: 72行:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/string/basic string/at]]
+
deenesfritplptruzh
[[en:cpp/string/basic string/at]]
+
[[es:cpp/string/basic string/at]]
+
[[fr:cpp/string/basic string/at]]
+
[[it:cpp/string/basic string/at]]
+
[[pl:cpp/string/basic string/at]]
+
[[pt:cpp/string/basic string/at]]
+
[[ru:cpp/string/basic string/at]]
+
[[zh:cpp/string/basic string/at]]
+

2020年2月18日 (火) 03:25時点における最新版

 
 
 
std::basic_string
 
reference       at( size_type pos );
(C++20未満)
constexpr reference       at( size_type pos );
(C++20以上)
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 - 返す文字の位置

[編集] 戻り値

要求された文字を指す参照。

[編集] 例外

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

[編集] 関連項目

指定された文字にアクセスします
(パブリックメンバ関数) [edit]