名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | string‎ | basic string
(Fix some translations)
1行: 1行:
{{tr_note}}
 
 
{{cpp/string/basic_string/title | at}}
 
{{cpp/string/basic_string/title | at}}
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
11行: 10行:
 
{{dcl end}}
 
{{dcl end}}
  
指定された位置{{tt|pos}}の文字への参照を返します。境界チェックが行われ、不正なアクセスに対して{{lc|std::out_of_range}}型の例外がスローされます.
+
指定された位置 {{tt|pos}} {{lc|std::out_of_range}}
  
===パラメータ===
+
======
 
{{par begin}}
 
{{par begin}}
{{par | pos | 取得する文字の位置}}
+
{{par | pos | }}
 
{{par end}}
 
{{par end}}
  
===値を返します===
+
======
{{tr|要求した文字への参照|reference to the requested character}}
+
  
 
===例外===
 
===例外===
{{c|1=pos >= size()}}の場合{{lc|std::out_of_range}}をスローします
+
{{c|1=pos >= size()}} の場合 {{lc|std::out_of_range}}
  
 
===計算量===
 
===計算量===
定数
+
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
 +
 +
 +
 
 +
 +
 +
 +
 
 +
 +
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 
 +
  
===参照===
 
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc | cpp/string/basic_string/dsc operator_at }}
 
{{dsc inc | cpp/string/basic_string/dsc operator_at }}

2018年3月27日 (火) 07:20時点における版

 
 
 
std::basic_string
 
reference       at( size_type pos );
const_reference at( size_type pos ) const;

指定された位置 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]