std::basic_string
| ヘッダ <string> で定義 |
||
| template< class CharT, |
(1) | |
| namespace pmr { template< |
(2) | (C++17以降) |
クラステンプレート basic_string は、文字のようなオブジェクトのシーケンスを格納し、操作します。これはTrivialTypeかつStandardLayoutTypeの非配列オブジェクトです。このクラスは、文字型にもその型に対する操作の性質にも依存しません。操作の定義は、Traits テンプレートパラメータ(std::char_traits の特殊化または互換性のある traits クラス)を介して提供されます。
basic_string の要素は連続して格納されます。つまり、basic_string s に対して、[0, s.size()) の任意の n について &*(s.begin() + n) == &*s.begin() + n であり、*(s.begin() + s.size()) は CharT() の値を持ちます(ヌル終端文字)(C++11以降)。あるいは、同等のこととして、s[0] へのポインタは、CharT の 配列(C++11まで)ヌル終端配列(C++11以降) の最初の要素へのポインタを期待する関数に渡すことができます。
std::basic_string は、AllocatorAwareContainer(ただし、要素の構築/破棄にカスタマイズされた construct/destroy は使用されません)、SequenceContainer、および ContiguousContainer(C++17以降)の要件を満たします。
Traits::char_type と Allocator::char_type のいずれかが CharT と異なる場合、プログラムは不適格となります。
|
|
(C++20以降) |
一般的な文字型のためにいくつかの typedef が提供されています
| ヘッダ
<string> で定義 | |
| 型 | 定義 |
std::string
|
std::basic_string<char> |
std::wstring
|
std::basic_string<wchar_t> |
std::u8string (C++20) |
std::basic_string<char8_t> |
std::u16string (C++11) |
std::basic_string<char16_t> |
std::u32string (C++11) |
std::basic_string<char32_t> |
std::pmr::string (C++17) |
std::pmr::basic_string<char> |
std::pmr::wstring (C++17) |
std::pmr::basic_string<wchar_t> |
std::pmr::u8string (C++20) |
std::pmr::basic_string<char8_t> |
std::pmr::u16string (C++17) |
std::pmr::basic_string<char16_t> |
std::pmr::u32string (C++17) |
std::pmr::basic_string<char32_t> |
目次 |
[編集] テンプレートパラメータ
| CharT | - | 文字型 |
| Traits | - | 文字型に対する操作を指定する traits クラス |
| アロケータ | - | 内部ストレージを割り当てるために使用される Allocator 型 |
[編集] ネストされた型
| 型 | 定義 | ||||
traits_type
|
Traits
| ||||
value_type
|
CharT
| ||||
allocator_type
|
Allocator | ||||
size_type
|
| ||||
difference_type
|
| ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
[編集] データメンバ
constexpr size_type npos [static] |
特別な値 size_type(-1)、その正確な意味は文脈に依存します |
[編集] メンバ関数
basic_string を構築する(public member function) | |
| 文字列を破棄し、使用されている場合は内部ストレージを解放する (public member function) | |
| 文字列に値を代入する (public member function) | |
| 文字列に文字を代入する (public member function) | |
| (C++23) |
文字列に文字の範囲を代入する (public member function) |
| 関連付けられたアロケータを返す (public member function) | |
要素アクセス | |
| 境界チェック付きで指定された文字にアクセスする (public member function) | |
| 指定された文字にアクセスする (public member function) | |
| (DR*) |
最初の文字にアクセスする (public member function) |
| (DR*) |
最後の文字にアクセスする (public member function) |
| 文字列の最初の文字へのポインタを返す (public member function) | |
| 文字列の変更不可能な標準C文字配列バージョンを返す (public member function) | |
| (C++17) |
文字列全体への変更不可能な basic_string_view を返す(public member function) |
イテレータ | |
| (C++11) |
先頭へのイテレータを返す (public member function) |
| (C++11) |
末尾へのイテレータを返す (public member function) |
| (C++11) |
先頭への逆イテレータを返す (public member function) |
| (C++11) |
末尾への逆イテレータを返す (public member function) |
容量 | |
| 文字列が空かどうかをチェックする (public member function) | |
| 文字数を返す (public member function) | |
| 最大文字数を返す (public member function) | |
| ストレージを予約する (public member function) | |
| 現在割り当てられているストレージに保持できる文字数を返す (public member function) | |
| (DR*) |
未使用のメモリを解放してメモリ使用量を削減する (public member function) |
変更 | |
| 内容をクリアする (public member function) | |
| 文字を挿入する (public member function) | |
| (C++23) |
文字の範囲を挿入する (public member function) |
| 文字を削除する (public member function) | |
| 末尾に文字を追加する (public member function) | |
| (DR*) |
最後の文字を削除する (public member function) |
| 末尾に文字を追加する (public member function) | |
| (C++23) |
末尾に文字の範囲を追加する (public member function) |
| 末尾に文字を追加する (public member function) | |
| 文字列の指定された部分を置換する (public member function) | |
| (C++23) |
文字列の指定された部分を文字の範囲で置換する (public member function) |
| 文字をコピーする (public member function) | |
| 格納されている文字数を変更する (public member function) | |
| (C++23) |
格納されている文字数を変更し、ユーザー提供の操作を介して不定な内容を上書きする可能性がある (public member function) |
| 内容を交換する (public member function) | |
検索 | |
| 指定された部分文字列が最初に現れる位置を見つける (public member function) | |
| 部分文字列が最後に現れる位置を見つける (public member function) | |
| 文字が最初に現れる位置を見つける (public member function) | |
| 文字が最初に現れない位置を見つける (public member function) | |
| 文字が最後に現れる位置を見つける (public member function) | |
| 文字が最後に現れない位置を見つける (public member function) | |
操作 | |
| 2つの文字列を比較する (public member function) | |
| (C++20) |
文字列が指定された接頭辞で始まるかをチェックする (public member function) |
| (C++20) |
文字列が指定された接尾辞で終わるかをチェックする (public member function) |
| (C++23) |
文字列が指定された部分文字列または文字を含むかをチェックする (public member function) |
| 部分文字列を返す (public member function) | |
[編集] 非メンバ関数
| 2つの文字列、文字列と char、または文字列と string_view を連結する (function template) | |
| (C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20) |
2つの文字列を辞書順で比較する (function template) |
| std::swap アルゴリズムを特殊化する (function template) | |
| 特定の基準を満たすすべての要素を削除する (関数テンプレート) | |
入出力 | |
| 文字列に対するストリーム入出力を実行する (function template) | |
| I/Oストリームから文字列にデータを読み込む (function template) | |
数値変換 | |
| (C++11)(C++11)(C++11) |
文字列を符号付き整数に変換する (function) |
| (C++11)(C++11) |
文字列を符号なし整数に変換する (function) |
| (C++11)(C++11)(C++11) |
文字列を浮動小数点値に変換する (function) |
| (C++11) |
整数値または浮動小数点値を string に変換する(function) |
| (C++11) |
整数値または浮動小数点値を wstring に変換する(function) |
[編集] リテラル
| インライン名前空間
std::literals::string_literals 内で定義 | |
| (C++14) |
文字配列リテラルを basic_string に変換する(function) |
[編集] ヘルパークラス
| (C++11) |
文字列のハッシュサポート (クラステンプレートの特殊化) |
[編集] 推論ガイド (C++17以降)
[編集] イテレータの無効化
basic_string の要素を参照する参照、ポインタ、イテレータは、std::getline、std::swap、または operator>> のように、非constの basic_string への参照を引数として取る任意の標準ライブラリ関数によって無効化される可能性があります。また、operator[], at, data, front, back, begin, rbegin, end, および rend を除く非constメンバ関数を呼び出すことによっても無効化される可能性があります。
[編集] ノート
C++23まで、std::basic_string の要素を構築または破棄する際にカスタマイズされた construct または destroy を使用することが要求されていましたが、全ての実装はデフォルトのメカニズムのみを使用していました。この要件は、既存の実践に合わせるために P1072R10 によって修正されました。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_string_udls |
201304L |
(C++14) | 文字列型のためのユーザー定義リテラル |
__cpp_lib_starts_ends_with |
201711L |
(C++20) | starts_with, ends_with |
__cpp_lib_constexpr_string |
201907L |
(C++20) | std::basic_string のための Constexpr |
__cpp_lib_char8_t |
201907L |
(C++20) | std::u8string
|
__cpp_lib_erase_if |
202002L |
(C++20) | erase、erase_if |
__cpp_lib_string_contains |
202011L |
(C++23) | contains
|
__cpp_lib_string_resize_and_overwrite |
202110L |
(C++23) | resize_and_overwrite
|
__cpp_lib_containers_ranges |
202202L |
(C++23) | コンテナ互換範囲を受け入れる構築、挿入、置換のためのメンバ関数 |
[編集] 例
#include <iostream> #include <string> int main() { using namespace std::literals; // Creating a string from const char* std::string str1 = "hello"; // Creating a string using string literal auto str2 = "world"s; // Concatenating strings std::string str3 = str1 + " " + str2; // Print out the result std::cout << str3 << '\n'; std::string::size_type pos = str3.find(" "); str1 = str3.substr(pos + 1); // the part after the space str2 = str3.substr(0, pos); // the part till the space std::cout << str1 << ' ' << str2 << '\n'; // Accessing an element using subscript operator[] std::cout << str1[0] << '\n'; str1[0] = 'W'; std::cout << str1 << '\n'; }
出力
hello world world hello w World
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 530 | C++98 | basic_string の要素のストレージの連続性LWG259 によって誤って必須ではなくなった |
再び必須になった |
| LWG 2861 | C++98 | value_type は Traits::char_type であった |
CharT に変更された |
| LWG 2994 (P1148R0) |
C++98 | Traits::char_type[1] のいずれかがおよび Allocator::char_type が CharT と異なる場合、振る舞いは未定義である |
プログラムは不適格となる この場合不適格となる |
[編集] 関連項目
| (C++17) |
読み取り専用の文字列ビュー (class template) |
[編集] 外部リンク
| C++ string handling |