名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | string
(Import from dokuwiki)
 
 
(5人の利用者による、間の21版が非表示)
1行: 1行:
{{title|C++ Strings}}
+
{{title|
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
++
 +
 +
 +
 +
 +
 +
 +
}}
  
Stringクラスは文字列を操作したり格納するのにとても便利なクラスです。Stringクラスは ''<string>'' ヘッダファイルの ''std'' 名前空間内で定義されています。
+
std
  
{| class="wikitable"
+
{|=||[[
|-
+
 
|[[cpp/string/basic_string/basic_string | コンストラクタ]]
+
|キャラクタ列や他の文字列から文字列を作成します
+
 
|-
+
|[[cpp/string/basic_string/operator_cmp | 演算子]]
+
|文字列の結合、文字列の割り当て、I/Oに対して文字列を扱う、文字列の比較を行う
+
string
|-
+
|[[cpp/string/basic_string/append | append]]
+
basic_string
|ある文字列に対してキャラクタや他の文字列を結合します
+
basic_string
|-
+
|
|[[cpp/string/basic_string/assign | assign]]
+
|
|キャラクタ列や他のC++文字列から文字列を生成します
+
|
|-
+
| stringbasic_string
|[[cpp/string/basic_string/at | at]]
+
|
|特定の位置のキャラクタを返します
+
|
|-
+
|
|[[cpp/string/basic_string/begin | begin]]
+
| basic_string
|文字列の先頭の指すイテレータを返します
+
|-
+
 
|[[cpp/string/basic_string/c_str | c_str]]
+
|文字列を変更不能な標準的な C キャラクタ配列に変換して返します
+
|-
+
|
|[[cpp/string/basic_string/capacity | capacity]]
+
|
|文字列が保持できるキャラクタ数を返します
+
|
|-
+
|[[cpp/string/basic_string/clear | clear]]
+
 
|文字列中のすべてのキャラクタを除去します
+
|-
+
|[[cpp/string/basic_string/compare | compare]]
+
|2つの文字列を比較します
+
|-
+
|[[cpp/string/basic_string/copy | copy]]
+
| cpp//basic_string
|文字列のキャラクタ列を配列にコピーします
+
/|
|-
+
| |
|[[cpp/string/basic_string/data | data]]
+
| cpp//basic_string
|ある文字列の先頭キャラクタを指すポインタを返します
+
/|
|-
+
| |
|[[cpp/string/basic_string/empty | empty]]
+
| cpp//basic_string
|文字列中に1キャラクタも無ければ true を返します
+
/|
|-
+
| |
|[[cpp/string/basic_string/end | end]]
+
| cpp//basic_string
|文字列の最後尾直後を指すイテレータを返します
+
/|
|-
+
|[[cpp/string/basic_string/erase | erase]]
+
 
|文字列からあるキャラクタ列を除去します
+
|-
+
|[[cpp/string/basic_string/find | find]]
+
| cpp/string/basic_string/
|文字列中でのキャラクタ列を探します
+
| |
|-
+
| cpp/string/basic_string/
|[[cpp/string/basic_string/find_first_not_of | find_first_not_of]]
+
|
|あるキャラクタ列が文字列中で見つからない先頭位置を返します
+
| cpp/string/basic_string/
|-
+
 
|[[cpp/string/basic_string/find_first_of | find_first_of]]
+
|
|あるキャラクタ列が文字列中で見つかる先頭位置を返します
+
|
|-
+
| cpp/string/basic_string/
|[[cpp/string/basic_string/find_last_not_of | find_last_not_of]]
+
|
|あるキャラクタ列が文字列中で見つからない最後尾を返します
+
|
|-
+
| cpp/string/basic_string/data
|[[cpp/string/basic_string/find_last_of | find_last_of]]
+
|
|あるキャラクタ列が文字列中で見つかる最後尾を返します
+
| cpp/string/basic_string/
|-
+
 
|[[cpp/string/basic_string/getline | getline]]
+
|
|I/Oストリームから読み込んだデータを文字列に挿入します
+
|
|-
+
| cpp/string/basic_string/end
|[[cpp/string/basic_string/insert | insert]]
+
|
|文字列中にキャラクタを挿入します
+
| cpp/string/basic_string/
|-
+
 
|[[cpp/string/basic_string/length | length]]
+
|
|文字列の長さを返します
+
| cpp/string/basic_string/
|-
+
|
|[[cpp/string/basic_string/max_size | max_size]]
+
|
|文字列が保持することができる最大キャラクタ数を返します
+
| cpp/string/basic_string/
|-
+
|
|[[cpp/string/basic_string/push_back | push_back]]
+
| cpp/string/basic_string/
|文字列の最後尾にキャラクタを追加します
+
 
|-
+
|
|[[cpp/string/basic_string/rbegin | rbegin]]
+
| cpp/string/basic_string/
|文字列の最後尾を指す逆イテレータを返します
+
|
|-
+
|
|[[cpp/string/basic_string/rend | rend]]
+
| cpp/string/basic_string/
|文字列の先頭を指す逆イテレータを返します
+
|
|-
+
|
|[[cpp/string/basic_string/replace | replace]]
+
| cpp/string/basic_string/
|文字列中のキャラクタを置換します
+
| /
|-
+
|
|[[cpp/string/basic_string/reserve | reserve]]
+
| cpp/string/basic_string/
|ある文字列の最小サイズをセットします
+
|
|-
+
|
|[[cpp/string/basic_string/resize | resize]]
+
| cpp/string/basic_string/
|文字列のサイズを変更します
+
|
|-
+
| cpp/string/basic_string/
|[[cpp/string/basic_string/rfind | rfind]]
+
 
|文字列の中で一番後ろにある部分列を探します
+
|
|-
+
| cpp/string/basic_string/
|[[cpp/string/basic_string/size | size]]
+
|
|文字列中の文字数を返します
+
|
|-
+
| cpp/string/basic_string/
|[[cpp/string/basic_string/substr | substr]]
+
|
|特定の箇所を抜き出して返します
+
|
|-
+
 
|[[cpp/string/basic_string/swap | swap]]
+
|
|ある文字列と他の文字列の中身をスワップします
+
| cpp/string/basic_string/
|-
+
|}
+
 
 +
 +
 +
|
 +
| cpp/string/basic_string/
 +
|
 +
|
 +
 
 +
|
 +
| cpp/string/basic_string/
 +
|
 +
 
 +
|
 +
| cpp/string/basic_string/
 +
|
 +
|
 +
| cpp/string/basic_string/
 +
|
 +
 +
 
 +
 +
 +
|
 +
| cpp/string/basic_string/
 +
 +
 
 +
 +
 +
| cpp/string/basic_string/
 +
 +
 
 +
[[cpp/string/basic_string/|]]
 +
 
 +
||}

2019年10月25日 (金) 07:55時点における最新版

 
 
 
std::basic_string
 
ヘッダ <string> で定義
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_string;
(1)
namespace pmr {

    template <class CharT, class Traits = std::char_traits<CharT>>
    using basic_string = std::basic_string< CharT, Traits,
                                            std::polymorphic_allocator<CharT>>

}
(2) (C++17以上)

クラステンプレート basic_stringchar ライクなオブジェクト (トリビアル標準レイアウト型の非配列オブジェクト) の並びを格納したり操作したりします。 このクラスは文字の型やその型の操作の性質には依存しません。 操作は Traits テンプレート引数で与えられる std::char_traits の特殊化またはそれと互換の特性クラスによって定義されます。 Traits::char_typeCharT は同じ型でなければならず、そうでなければプログラムは ill-formed です。

basic_string の要素は隣接して格納されます,。 つまり、 basic_string s に対して、 [0, s.size()) の任意の n について &*(s.begin() + n) == &*s.begin() + n です。 別の言い方をすると、ヌル終端 (C++11以上) CharT[] 配列の最初の要素へのポインタを期待する関数に s[0] へのポインタを渡すことができます。

std::basic_stringAllocatorAwareContainer, SequenceContainer および ContiguousContainer (C++17以上) の要件を満たします。

一般的な文字型に対していくつかの 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 - 文字型の操作を指定する型特性クラス
Allocator - 内部の記憶域を確保するために使用されるアロケータ

[編集] メンバ型

メンバ型 定義
traits_type Traits
value_type CharT
allocator_type Allocator [edit]
size_type
Allocator::size_type (C++11未満)
std::allocator_traits<Allocator>::size_type (C++11以上)
[edit]
difference_type
Allocator::difference_type (C++11未満)
std::allocator_traits<Allocator>::difference_type (C++11以上)
[edit]
reference
Allocator::reference (C++11未満)
value_type& (C++11以上)
[edit]
const_reference
Allocator::const_reference (C++11未満)
const value_type& (C++11以上)
[edit]
pointer
Allocator::pointer (C++11未満)
std::allocator_traits<Allocator>::pointer (C++11以上)
[edit]
const_pointer
Allocator::const_pointer (C++11未満)
std::allocator_traits<Allocator>::const_pointer (C++11以上)
[edit]
iterator LegacyRandomAccessIterator [edit]
const_iterator const LegacyRandomAccessIterator [edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

[編集] メンバ関数

basic_string を構築します
(パブリックメンバ関数) [edit]
デストラクタ
内部の記憶域を解放し、文字列を破棄します
(パブリックメンバ関数)
文字列に値を代入します
(パブリックメンバ関数) [edit]
文字列に文字を代入します
(パブリックメンバ関数) [edit]
関連付けられているアロケータを返します
(パブリックメンバ関数) [edit]
要素アクセス
境界チェック付きで指定された文字にアクセスします
(パブリックメンバ関数) [edit]
指定された文字にアクセスします
(パブリックメンバ関数) [edit]
(C++11)
最初の文字にアクセスします
(パブリックメンバ関数) [edit]
(C++11)
最後の文字にアクセスします
(パブリックメンバ関数) [edit]
文字列の最初の文字を指すポインタを返します
(パブリックメンバ関数) [edit]
文字列の変更不可能な標準 C の文字配列バージョンを返します
(パブリックメンバ関数) [edit]
文字列全体に対する変更不可能な string_view を返します
(パブリックメンバ関数) [edit]
イテレータ
先頭を指すイテレータを返します
(パブリックメンバ関数) [edit]
(C++11)
終端を指すイテレータを返します
(パブリックメンバ関数) [edit]
先頭を指す逆イテレータを返します
(パブリックメンバ関数) [edit]
終端を指す逆イテレータを返します
(パブリックメンバ関数) [edit]
容量
文字列が空かどうか調べます
(パブリックメンバ関数) [edit]
文字数を返します
(パブリックメンバ関数) [edit]
最大文字数を返します
(パブリックメンバ関数) [edit]
記憶域を予約します
(パブリックメンバ関数) [edit]
現在確保されている記憶域に保持することができる文字数を返します
(パブリックメンバ関数) [edit]
未使用のメモリを解放してメモリ使用量を減らします
(パブリックメンバ関数) [edit]
操作
内容をクリアします
(パブリックメンバ関数) [edit]
文字を挿入します
(パブリックメンバ関数) [edit]
文字を削除します
(パブリックメンバ関数) [edit]
文字を末尾に追加します
(パブリックメンバ関数) [edit]
(C++11)
最後の文字を削除します
(パブリックメンバ関数) [edit]
文字を末尾に追加します
(パブリックメンバ関数) [edit]
文字を末尾に追加します
(パブリックメンバ関数) [edit]
2つの文字列を比較します
(パブリックメンバ関数) [edit]
文字列が指定された接頭辞で始まるか調べます
(パブリックメンバ関数) [edit]
(C++20)
文字列が指定された接尾辞で終わるか調べます
(パブリックメンバ関数) [edit]
文字列の指定された部分を置換します
(パブリックメンバ関数) [edit]
部分文字列を返します
(パブリックメンバ関数) [edit]
文字をコピーします
(パブリックメンバ関数) [edit]
格納されている文字の数を変更します
(パブリックメンバ関数) [edit]
内容を入れ替えます
(パブリックメンバ関数) [edit]
検索
文字列内の文字を探します
(パブリックメンバ関数) [edit]
部分文字列が現れる最後の位置を探します
(パブリックメンバ関数) [edit]
文字が現れる最初の位置を探します
(パブリックメンバ関数) [edit]
文字が現れない最初の位置を探します
(パブリックメンバ関数) [edit]
文字が現れる最後の位置を探します
(パブリックメンバ関数) [edit]
文字が現れない最後の位置を探します
(パブリックメンバ関数) [edit]

定数

[静的]
特殊な値です。正確な意味は文脈に依存します
(パブリック静的メンバ定数) [edit]

[編集] 非メンバ関数

2つの文字列または文字列と文字を連結します
(関数テンプレート) [edit]
(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20)
2つの文字列を辞書的に比較します
(関数テンプレート) [edit]
std::swap アルゴリズムの特殊化
(関数テンプレート) [edit]
特定の基準を満たすすべての要素を削除します
(関数テンプレート) [edit]
入出力
文字列のストリーム入出力を行います
(関数テンプレート) [edit]
入出力ストリームから文字列にデータを読み込みます
(関数) [edit]
数値変換
(C++11)(C++11)(C++11)
文字列を符号付き整数に変換します
(関数) [edit]
(C++11)(C++11)
文字列を符号なし整数に変換します
(関数) [edit]
(C++11)(C++11)(C++11)
文字列を浮動小数点値に変換します
(関数) [edit]
(C++11)
整数または浮動小数点値を string に変換します
(関数) [edit]
整数または浮動小数点値を wstring に変換します
(関数) [edit]

[編集] リテラル

名前空間 std::literals::string_literals で定義
文字配列リテラルを basic_string に変換します
(関数) [edit]

[編集] ヘルパークラス

文字列に対するハッシュサポート
(クラステンプレートの特殊化) [edit]

[編集] 推定ガイド(C++17以上)