std::basic_string<CharT,Traits,Allocator>::basic_string
(1) | ||
basic_string(); explicit basic_string( const Allocator& alloc ); |
(C++17未満) | |
basic_string() noexcept(noexcept( Allocator() )) : basic_string( Allocator() ) {} |
(C++17以上) (C++20未満) |
|
constexpr basic_string() noexcept(noexcept( Allocator() )) : basic_string( Allocator() ) {} |
(C++20以上) | |
(2) | ||
basic_string( size_type count, CharT ch, |
(C++20未満) | |
constexpr basic_string( size_type count, CharT ch, |
(C++20以上) | |
(3) | ||
basic_string( const basic_string& other, size_type pos, |
(C++17未満) | |
basic_string( const basic_string& other, size_type pos, |
(C++17以上) (C++20未満) |
|
constexpr basic_string( const basic_string& other, size_type pos, |
(C++20以上) | |
(3) | ||
basic_string( const basic_string& other, size_type pos, |
(C++17以上) (C++20未満) |
|
constexpr basic_string( const basic_string& other, size_type pos, |
(C++20以上) | |
(4) | ||
basic_string( const CharT* s, size_type count, |
(C++20未満) | |
constexpr basic_string( const CharT* s, size_type count, |
(C++20以上) | |
(5) | ||
basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
(C++20未満) | |
constexpr basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
(C++20以上) | |
(6) | ||
template< class InputIt > basic_string( InputIt first, InputIt last, |
(C++20未満) | |
template< class InputIt > constexpr basic_string( InputIt first, InputIt last, |
(C++20以上) | |
(7) | ||
basic_string( const basic_string& other ); |
(C++20未満) | |
constexpr basic_string( const basic_string& other ); |
(C++20以上) | |
(7) | ||
basic_string( const basic_string& other, const Allocator& alloc ); |
(C++11以上) (C++20未満) |
|
constexpr basic_string( const basic_string& other, const Allocator& alloc ); |
(C++20以上) | |
(8) | ||
basic_string( basic_string&& other ) noexcept; |
(C++11以上) (C++20未満) |
|
constexpr basic_string( basic_string&& other ) noexcept; |
(C++20以上) | |
(8) | ||
basic_string( basic_string&& other, const Allocator& alloc ); |
(C++11以上) (C++20未満) |
|
constexpr basic_string( basic_string&& other, const Allocator& alloc ); |
(C++20以上) | |
(9) | ||
basic_string( std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator() ); |
(C++11以上) (C++20未満) |
|
constexpr basic_string( std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator() ); |
(C++20以上) | |
(10) | ||
template < class T > explicit basic_string( const T& t, const Allocator& alloc = Allocator() ); |
(C++17以上) (C++20未満) |
|
template < class T > explicit constexpr basic_string( const T& t, |
(C++20以上) | |
(11) | ||
template < class T > basic_string( const T& t, size_type pos, size_type n, |
(C++17以上) (C++20未満) |
|
template < class T > constexpr basic_string( const T& t, size_type pos, size_type n, |
(C++20以上) | |
様々なデータソースと、オプショナルなユーザ供給のアロケータ alloc
から、新しい文字列を構築します。
ch
のコピー count
個を持つ文字列を構築します。 このコンストラクタは、推定される Allocator
型がアロケータとして適格でない場合、クラステンプレートの実引数推定に対して使用されません。 (C++17以上)other
の部分文字列 [pos, pos+count)
を持つ文字列を構築します。 count == npos の場合、 count
が指定されない場合、または要求された部分文字列が文字列の終端を超える場合、結果の部分文字列は [pos, other.size())
になります。s
の指す文字列の最初の count
個の文字を持つ文字列を構築します。 s
はヌル文字を含むこともできます。 文字列の長さは count
です。 [s, s + count)
が有効な範囲でない場合、動作は未定義です。s
の指すヌル終端文字列のコピーで初期化された内容を持つ文字列を構築します。 文字列の長さは最初のヌル文字によって決まります。 [s, s + Traits::length(s))
が有効な範囲でない場合 (例えば s
がヌルポインタの場合)、動作は未定義です。 このコンストラクタは、推定される Allocator
型がアロケータとして適格でない場合、クラステンプレートの実引数推定に対して使用されません。 (C++17以上)[first, last)
の内容を持つ文字列を構築します。
|
(C++11未満) |
このコンストラクタは、 |
(C++11以上) |
other
の内容のコピーを持つ文字列を構築します。other
の内容を持つ文字列を構築します。 other
は有効だけれども未規定な状態になります。ilist
の内容を持つ文字列を構築します。t
を文字列ビュー sv
に暗黙に変換し、 basic_string(sv.data(), sv.size(), alloc) によって行われたかのように、 sv
の内容を持つ文字列を初期化します。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true であり、 std::is_convertible_v<const T&, const CharT*> が false である場合にのみ、オーバーロード解決に参加します。t
を文字列ビュー sv
に暗黙に変換し、 basic_string(sv.substr(pos, n), a) によって行われたかのように、 sv
の部分範囲 [pos, pos + n)
を持つ文字列を初期化します。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> が true である場合にのみ、オーバーロード解決に参加します。目次 |
[編集] 引数
alloc | - | この文字列のすべてのメモリ確保のために使用するアロケータ |
count | - | 結果の文字列のサイズ |
ch | - | 文字列を初期化するための値 |
pos | - | 含める最初の文字の位置 |
first, last | - | 文字をコピーする範囲 |
s | - | 文字列を初期化するためのソースとして使用する文字配列を指すポインタ |
other | - | 文字列を初期化するためのソースとして使用する別の文字列 |
ilist | - | 文字列を初期化するための std::initializer_list |
t | - | 文字列を初期化するための (std::basic_string_view に変換可能な) オブジェクト |
[編集] 計算量
count
に比例。s
の長さに比例。first
と last
の距離に比例。other
のサイズに比例。alloc
が指定されていて alloc != other.get_allocator() の場合は比例。ilist
のサイズに比例。[編集] 例外
構築される文字列の長さが max_size() を超えるであろう場合 (例えば (2) に対して count > max_size() の場合) は std::length_error を投げます。Allocator::allocate
の呼び出しが例外を投げるかもしれません。
[編集] ノート
途中に '\0' を持つ文字列リテラルの初期化はオーバーロード (5) を使用し、最初のヌル文字で停止します。 これは別のコンストラクタを指定するか、 operator""s を使用することによって回避できます。
std::string s1 = "ab\0\0cd"; // s1 contains "ab" std::string s2{"ab\0\0cd", 6}; // s2 contains "ab\0\0cd" std::string s3 = "ab\0\0cd"s; // s3 contains "ab\0\0cd"
[編集] 欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
DR | 適用先 | 発行時の動作 | 正しい動作 |
---|---|---|---|
LWG 2193 | C++11 | the default constructor is explicit | made non-explicit |
LWG 2946 | C++17 | string_view overload causes ambiguity in some cases
|
avoided by making it a template |
LWG 3076 | C++17 | two constructors may cause ambiguities in class template argument deduction | constrained |
[編集] 例
#include <iostream> #include <cassert> #include <iterator> #include <string> #include <cctype> int main() { { // string::string() std::string s; assert(s.empty() && (s.length() == 0) && (s.size() == 0)); } { // string::string(size_type count, charT ch) std::string s(4, '='); std::cout << s << '\n'; // "====" } { std::string const other("Exemplary"); // string::string(string const& other, size_type pos, size_type count) std::string s(other, 0, other.length()-1); std::cout << s << '\n'; // "Exemplar" } { // string::string(charT const* s, size_type count) std::string s("C-style string", 7); std::cout << s << '\n'; // "C-style" } { // string::string(charT const* s) std::string s("C-style\0string"); std::cout << s << '\n'; // "C-style" } { char mutable_c_str[] = "another C-style string"; // string::string(InputIt first, InputIt last) std::string s(std::begin(mutable_c_str)+8, std::end(mutable_c_str)-1); std::cout << s << '\n'; // "C-style string" } { std::string const other("Exemplar"); std::string s(other); std::cout << s << '\n'; // "Exemplar" } { // string::string(string&& str) std::string s(std::string("C++ by ") + std::string("example")); std::cout << s << '\n'; // "C++ by example" } { // string(std::initializer_list<charT> ilist) std::string s({ 'C', '-', 's', 't', 'y', 'l', 'e' }); std::cout << s << '\n'; // "C-style" } { // overload resolution selects string(InputIt first, InputIt last) [with InputIt = int] // which behaves as if string(size_type count, charT ch) is called std::string s(3, std::toupper('a')); std::cout << s << '\n'; // "AAA" } }
出力:
==== Exemplar C-style C-style C-style string Exemplar C++ by example C-style AAA
[編集] 関連項目
文字列に文字を代入します (パブリックメンバ関数) | |
文字列に値を代入します (パブリックメンバ関数) | |
(C++11) |
整数または浮動小数点値を string に変換します (関数) |
(C++11) |
整数または浮動小数点値を wstring に変換します (関数) |