#topicpath(); c++11 相当の文字列型全般 ////////////////////////////////////////////////////////////////////////////// * 目次 [#f82a4887] #contents(); ////////////////////////////////////////////////////////////////////////////// * コンストラクタ [#j24451d7] |~example code |~description |~remark | |string a; |空の文字列を生成 || |string b = "example"; | 右辺に渡された文字列で生成 || |string c( b, 1, 3 ); |元になる文字列 b の1番目から3番目の部分文字列から文字列を生成 || |string d( "example", 2 ); |文字列先頭2文字からなる文字列を生成 || |string e( 10, 't' ); |10個の文字 't' からなる文字列を生成 || |string f = { 'e, 'x', 'a' }; |文字の初期化子リストから文字列を生成|| |string g = b; |string 変数 b のコピーを構築 || |string i( b.begin(), b.end() ); |string 変数 b の iterator によって指定された範囲から文字列を生成 |逆順を指定すると、gcc 4.9.2 では basic_string::_S_create が "std::length_error" でコアダンプする | ////////////////////////////////////////////////////////////////////////////// * 比較 [#ze22d38d] - 2つの string 変数同士の比較には、以下の演算子が使用出来る == != < > <= >= - 大小比較については文字コード順に行われる。 ////////////////////////////////////////////////////////////////////////////// * 連結 [#wb027a67] - 文字列同士の連結には + 演算子を使用する ////////////////////////////////////////////////////////////////////////////// * 文字列長の取得 [#gf9e5d1c] - 以下の2つの関数がある。これらに挙動の差異はない。 -- size_type std::basic_string::length() -- size_type std::basic_string::size() ////////////////////////////////////////////////////////////////////////////// * 文字列を空にする [#qf83482d] - void std::basic_string::clear() ////////////////////////////////////////////////////////////////////////////// * empty(): 空文字かどうかを判定する [#n0279c8f] - bool std::base_string::empty() ////////////////////////////////////////////////////////////////////////////// * 文字列ポインタを取得する [#y0eb62c6] - std::basic_string::c_str() -- 末尾の NULL 文字を含めた文字列を返す。 - std::basic_string::data() -- C++11になる前までは、末尾のNULL文字を含めない仕様になっていた。 - c_str(), data() とも、''文字列オブジェクトの内容が変化した時には無効になる'' ////////////////////////////////////////////////////////////////////////////// * iterator による操作 [#f3406696] - 1文字ごとに操作 - 順方向 : begin(), end() -- 返されるiterator が const になるか否かは、オブジェクトの状態によって異なる。 - 逆順方向: rbegin(), rend() - const_iterator:cbegin(), cend() -- オブジェクトの状態に関係なく const_iterator を返す ////////////////////////////////////////////////////////////////////////////// * 部分文字列の取得 [#q04f94f0] - 書式: basic_str::substr( size_type pos = 0, size_type n = npos ) const; -- pos: 取得したい部分の先頭 index -- n : 取得する長さ - pos > size() の場合、std::out_of_range 例外が送出される ////////////////////////////////////////////////////////////////////////////// * 数値 ←→文字列変換 [#d86b4551] - [[数値→文字列>Lang/C++/C++11#to_string]] - [[文字列→数値>Lang/C++/C++11#sto]] ////////////////////////////////////////////////////////////////////////////// * 文字列検索 [#search-str] //============================================================================ ** 文字列検索し、最初に見つかった位置を取得する [#find] - 書式: size_type find( const basic_string& str, size_type pos = 0 ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( const CharT* s, size_type pos = 0 ) const noexcept; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type find( CharT c, size_type pos = 0 ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ //============================================================================ ** 文字列検索し、最後に見つかった位置を取得する [#rfind] - 書式: size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; str: 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos, size_type n ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( const CharT* s, size_type pos = npos ) const; s : 検索文字列 pos: 検索開始位置 n : 検索する文字列の長さ size_type rfind( CharT c, size_type pos = npos ) const noexcept; c : 検索文字 pos: 検索開始位置 n : 検索する文字列の長さ - rfind() の pos は、検索範囲のうち最も大きな index を指定する //============================================================================ ** 文字群の中から最初に見つかった index を取得する [#e056c189] - 書式: //============================================================================ ** 文字群の中から最後に見つかった index を取得する [#fc698687] - 書式: ////////////////////////////////////////////////////////////////////////////// * std::wstring [#wstring] - wchar_t 型文字列 - 文字コードの規定はないが、wchat_t が2バイトの環境では UTF-16 の文字列として使われてきた。 ////////////////////////////////////////////////////////////////////////////// * std::u16string [#u16string] - char16_t 型文字列 - 文字コードは UTF-16 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]] ////////////////////////////////////////////////////////////////////////////// * std::u32string [#u32string] - char32_t 型文字列 - 文字コードは UTF-32 - see also: [[std::string>Lang/C++/コンテナ#string]], [[std::wstring>Lang/C++/コンテナ#wstring]]