Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: basic_string

From cppreference.net
std::basic_string
HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++20以降のconstexpr指定に関する説明のみを日本語に翻訳しました。 HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++20以降でconstexprが利用可能であることを示すテキストのみを日本語に翻訳しました。 HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++20以降でconstexpr指定子が追加されたことを示す注記のみを日本語で表示しています。 HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++23以降を示すテキストのみを日本語に翻訳しました。
(1)
basic_string ( ) : basic_string ( Allocator ( ) ) { }
(C++11以降)
(C++17まで)
basic_string ( ) noexcept ( noexcept ( Allocator ( ) ) )
: basic_string ( Allocator ( ) ) { }
(C++17以降)
(C++20以降constexpr)
(2)
explicit basic_string ( const Allocator & alloc = Allocator ( ) ) ;
(C++11まで)
explicit basic_string ( const Allocator & alloc ) ;
(C++17以降noexcept)
(C++20以降constexpr)
basic_string ( size_type count, CharT ch,
const Allocator & alloc = Allocator ( ) ) ;
(3) (C++20以降 constexpr)
template < class InputIt >

basic_string ( InputIt first, InputIt last,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr C++20以降)
template < container-compatible-range < CharT > R >

constexpr basic_string ( std:: from_range_t , R && rg,

const Allocator & = Allocator ( ) ) ;
(5) (C++23以降)
basic_string ( const CharT * s, size_type count,
const Allocator & alloc = Allocator ( ) ) ;
(6) (constexpr since C++20)
basic_string ( const CharT * s, const Allocator & alloc = Allocator ( ) ) ;
(7) (constexpr since C++20)
basic_string ( std:: nullptr_t ) = delete ;
(8) (C++23以降)
template < class StringViewLike >

explicit basic_string ( const StringViewLike & t,

const Allocator & alloc = Allocator ( ) ) ;
(9) (C++17以降)
(C++20以降 constexpr)
template < class StringViewLike >

basic_string ( const StringViewLike & t,
size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(10) (C++17以降)
basic_string ( const basic_string & other ) ;
(11) (C++20以降 constexpr)
basic_string ( basic_string && other ) noexcept ;
(12) (C++11以降)
(C++20以降 constexpr)
basic_string ( const basic_string & other, const Allocator & alloc ) ;
(13) (C++11以降)
(C++20以降 constexpr)
basic_string ( basic_string && other, const Allocator & alloc ) ;
(14) (C++11以降)
(C++20以降constexpr)
basic_string ( const basic_string & other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ;
(15) (constexpr C++20以降)
constexpr basic_string ( basic_string && other, size_type pos,
const Allocator & alloc = Allocator ( ) ) ;
(16) (C++23以降)
basic_string ( const basic_string & other,

size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(17) (constexpr since C++20)
constexpr basic_string ( basic_string && other,

size_type pos, size_type count,

const Allocator & alloc = Allocator ( ) ) ;
(18) (C++23以降)
basic_string ( std:: initializer_list < CharT > ilist,
const Allocator & alloc = Allocator ( ) ) ;
(19) (C++11以降)
(C++20以降constexpr)

様々なデータソースから新しい文字列を構築し、オプションでユーザー提供のアロケータ alloc を使用します。

1) C++11以降のデフォルトコンストラクタ。デフォルト構築されたアロケータで空の文字列を構築します。
Allocatorが DefaultConstructible でない場合、動作は未定義です。
2) C++11までのデフォルトコンストラクタ。指定されたアロケータ alloc で空の文字列を構築します。
3) 文字 ch count 個複製した文字列を構築する。

CharT CopyInsertable を満たさない場合、動作は未定義である。

(C++11以降)

このオーバーロードは、 Allocator Allocator の要件を満たす場合にのみ、オーバーロード解決に参加する。

(C++17以降)
4) 範囲 [ first , last ) の内容を持つ文字列を構築します。 [ first , last ) 内の各イテレータは正確に1回デリファレンスされます。

InputIt LegacyInputIterator の要件を満たさない場合、オーバーロード (3) が引数 static_cast < size_type > ( first ) last alloc で代わりに呼び出されます。

(C++11以前)

このオーバーロードは、 InputIt LegacyInputIterator の要件を満たす場合にのみオーバーロード解決に参加します。

CharT std:: basic_string < CharT > * first から EmplaceConstructible でない場合、動作は未定義です。

(C++11以降)
5) 範囲 rg の内容を持つ文字列を構築します。 rg 内の各イテレータは正確に1回デリファレンスされます。
CharT EmplaceConstructible でない場合、 std:: basic_string < CharT > * ranges:: begin ( rg ) から構築できない場合、動作は未定義です。
6) 範囲の内容を持つ文字列を構築します [ s , s + count )
もし [ s , s + count ) が有効な範囲でない場合、動作は未定義です。
7) 次と等価 basic_string ( s, Traits :: length ( s ) , alloc )

このオーバーロードは、 Allocator Allocator の要件を満たす場合にのみオーバーロード解決に参加します。

(C++17以降)
8) std::basic_string nullptr から構築できません。
9) t を文字列ビュー sv に暗黙変換し( std:: basic_string_view < CharT, Traits > sv = t ; による)、その後 basic_string ( sv. data ( ) , sv. size ( ) , alloc ) によって文字列を構築する。
このオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します: std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > false である場合です。
10) t を文字列ビュー sv に暗黙的に変換し( std:: basic_string_view < CharT, Traits > sv = t ; の要領)、その後 basic_string ( sv. substr ( pos, n ) , alloc ) のように文字列を構築する。
このオーバーロードは、以下の条件が std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true である場合にのみ、オーバーロード解決に参加します。
11-18) other の(一部の)内容を持つ文字列を構築します。 other の型が basic_string&& の場合、構築完了時には other は有効だが未規定の状態になります。
11) コピーコンストラクタ。

アロケータは、以下の呼び出しによって取得されるかのように取得される: std:: allocator_traits < Allocator > ::
select_on_container_copy_construction
( other. get_allocator ( ) )

(C++11以降)
12) ムーブコンストラクタ。アロケータは other. get_allocator ( ) からのムーブ構築によって取得される。
13) コピーコンストラクタと同じですが、 alloc がアロケータとして使用される点が異なります。
CharT CopyInsertable でない場合、 std:: basic_string < CharT > への挿入は未定義動作となります。
14) ムーブコンストラクタと同じですが、 alloc がアロケータとして使用される点が異なります。
CharT MoveInsertable でない場合、 std:: basic_string < CharT > への挿入は未定義動作となります。
15,16) 範囲の内容を持つ文字列を構築します [ other. data ( ) + pos , other. data ( ) + other. size ( ) ) .
17,18) 範囲の内容を持つ文字列を構築します [ other. data ( ) + pos , other. data ( ) + ( pos + std:: min ( count, other. size ( ) - pos ) ) ) .
19) 次と等価: basic_string ( ilist. begin ( ) , ilist. end ( ) )

目次

翻訳のポイント: - 「Contents」を「目次」に翻訳 - C++関連の専門用語(Parameters, Complexity, Exceptions, Notes, Example, Defect reports, See also)は原文のまま保持 - HTMLタグ、属性、クラス名、IDは一切変更せず - 数値、リンク、構造は完全に保持 - プロフェッショナルな技術文書としての正確性を維持

パラメータ

alloc - この文字列のすべてのメモリ割り当てに使用するアロケータ
count - 結果の文字列のサイズ
ch - 文字列の初期化に使用する値
pos - 含める最初の文字の位置
first, last - 文字をコピーする範囲
s - 文字列の初期化に使用する文字配列へのポインタ
other - 文字列の初期化に使用する別の文字列
ilist - std::initializer_list 文字列の初期化に使用する
t - 文字列の初期化に使用するオブジェクト( std::basic_string_view に変換可能)
rg - コンテナ互換の範囲

計算量

1,2) 定数。
3-7) 文字列のサイズに対して線形。
9-11) 文字列のサイズに対して線形。
12) 定数。
13) 文字列のサイズに対して線形。
14) 文字列のサイズに対して線形( alloc ! = other. get_allocator ( ) true の場合)、それ以外の場合は定数時間。
15-19) 文字列のサイズに対して線形。

例外

10) std::out_of_range 範囲外の場合 pos が範囲外の場合。
14) alloc == str. get_allocator ( ) true の場合、例外を投げない。
15-18) std::out_of_range if pos > other. size ( ) is true .

構築される文字列の長さが std::length_error をスローする(例えば max_size() を超える場合( count > max_size ( ) (3) の場合)。 Allocator::allocate の呼び出しは例外をスローする可能性がある。

何らかの理由で例外がスローされた場合、これらの関数は何も効果を持ちません( strong exception safety guarantee )。

注記

埋め込まれた ' \0 ' 文字を含む string literal による初期化は、最初のnull文字で停止するオーバーロード (7) を使用します。これは、異なるコンストラクタを指定するか、 operator""s を使用することで回避できます:

std::string s1 = "ab\0\0cd";   // s1は"ab"を含む
std::string s2{"ab\0\0cd", 6}; // s2は"ab\0\0cd"を含む
std::string s3 = "ab\0\0cd"s;  // s3は"ab\0\0cd"を含む
機能テスト マクロ 標準 機能
__cpp_lib_containers_ranges 202202L (C++23) タグ付きコンストラクタ (5) による コンテナ互換範囲 からの構築

#include <cassert>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
    std::cout << "1)  string(); ";
    std::string s1;
    assert(s1.empty() && (s1.length() == 0) && (s1.size() == 0));
    std::cout << "s1.capacity(): " << s1.capacity() << '\n'; // 未指定
    std::cout << "3)  string(size_type count, CharT ch): ";
    std::string s2(4, '=');
    std::cout << std::quoted(s2) << '\n'; // "===="
    std::cout << "4)  string(InputIt first, InputIt last): ";
    char mutable_c_str[] = "別のCスタイル文字列";
    std::string s4(std::begin(mutable_c_str) + 8, std::end(mutable_c_str) - 1);
    std::cout << std::quoted(s4) << '\n'; // "C-style string"
    std::cout << "6)  string(CharT const* s, size_type count): ";
    std::string s6("C-style string", 7);
    std::cout << std::quoted(s6) << '\n'; // "Cスタイル"、すなわち [0, 7)
    std::cout << "7)  string(CharT const* s): ";
    std::string s7("Cスタイル\0文字列");
    std::cout << std::quoted(s7) << '\n'; // "C-style"
    std::cout << "11) string(string&): ";
    std::string const other11("模範");
    std::string s11(other11);
    std::cout << std::quoted(s11) << '\n'; // "Exemplar"
    std::cout << "12) string(string&&): ";
    std::string s12(std::string("C++ by ") + std::string("example"));
    std::cout << std::quoted(s12) << '\n'; // "C++ by example"
    std::cout << "15) string(const string& other, size_type pos): ";
    std::string const other15("Mutatis Mutandis");
    std::string s15(other15, 8);
    std::cout << std::quoted(s15) << '\n'; // "Mutandis"、すなわち [8, 16)
    std::cout << "17) string(const string& other, size_type pos, size_type count): ";
    std::string const other17("模範的");
    std::string s17(other17, 0, other17.length() - 1);
    std::cout << std::quoted(s17) << '\n'; // "Exemplar"
    std::cout << "19) string(std::initializer_list<CharT>): ";
    std::string s19({'C', '-', 's', 't', 'y', 'l', 'e'});
    std::cout << std::quoted(s19) << '\n'; // "C-style"
}

出力例:

1)  string(); s1.capacity(): 15
3)  string(size_type count, CharT ch): "===="
4)  string(InputIt first, InputIt last): "C-style string"
6)  string(CharT const* s, size_type count): "C-style"
7)  string(CharT const* s): "C-style"
11) string(string&): "Exemplar"
12) string(string&&): "C++ by example"
15) string(const string& other, size_type pos): "Mutandis"
17) string(const string& other, size_type pos, size_type count): "Exemplar"
19) string(std::initializer_list<CharT>): "C-style"

不具合報告

以下の動作変更に関する欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 301 C++98 オーバーロード (4) はパラメータ
alloc を使用しなかった( InputIt が整数型の場合)
そのパラメータを使用する
LWG 438 C++98 オーバーロード (4) InputIt が整数型の場合のみ
オーバーロード (3) を呼び出した
InputIt LegacyInputIterator でない場合に
オーバーロード (3) を呼び出す
LWG 847 C++98 例外安全性保証が存在しなかった 強い例外安全性保証を追加
LWG 2193 C++11 デフォルトコンストラクタがexplicit non-explicitに変更
LWG 2235 C++98 s がnullポインタ値である可能性があった この場合の動作は未定義
LWG 2250 C++98 オーバーロード (17) の動作は未定義
pos > other. size ( ) true の場合)
この場合常に例外を
スローする
LWG 2583 C++98 オーバーロード (17) にアロケータを提供する方法がなかった オーバーロード (15) を追加
LWG 2946 C++17 オーバーロード (9) は一部の場合に曖昧さを引き起こす テンプレート化することで回避
LWG 3076 C++17 オーバーロード (3,7) はクラステンプレート引数推論で
曖昧さを引き起こす可能性がある
制約を追加
LWG 3111
( P1148R0 )
C++98 LWG issue 2235 の解決により
basic_string ( nullptr, 0 ) の動作が未定義となった
この場合の動作は
明確に定義される [1]
  1. [ nullptr , nullptr + 0 ) は有効な空範囲です。なぜなら、nullポインタ値にゼロを加えることも 明確に定義されている からです(結果は依然としてnullポインタ値となります)。

関連項目

文字列に文字を割り当てる
(公開メンバ関数)
文字列に値を代入する
(公開メンバ関数)
(C++11)
整数値または浮動小数点値を string に変換する
(関数)
(C++11)
整数値または浮動小数点値を wstring に変換する
(関数)
basic_string_view を構築する
( std::basic_string_view<CharT,Traits> の公開メンバ関数)