std::basic_string::basic_string
提供: cppreference.com
< cpp | string | basic string
(1) | ||
basic_string(); explicit basic_string( const Allocator& alloc ); |
(C++17以前) | |
basic_string() noexcept(noexcept( Allocator() )): basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc ) noexcept; |
(C++17およびそれ以降) | |
basic_string( size_type count, CharT ch, |
(2) | |
(3) | ||
basic_string( const basic_string& other, size_type pos, |
(C++17以前) | |
basic_string( const basic_string& other, size_type pos, |
(C++17およびそれ以降) | |
basic_string( const basic_string& other, size_type pos, |
(C++17およびそれ以降) | |
basic_string( const CharT* s, size_type count, |
(4) | |
basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
(5) | |
template< class InputIt > basic_string( InputIt first, InputIt last, |
(6) | |
basic_string( const basic_string& other ); |
(7) | |
basic_string( const basic_string& other, const Allocator& alloc ); |
(7) | (C++11およびそれ以降) |
basic_string( basic_string&& other ) noexcept; |
(8) | (C++11およびそれ以降) |
basic_string( basic_string&& other, const Allocator& alloc ); |
(8) | (C++11およびそれ以降) |
basic_string( std::initializer_list<CharT> init, const Allocator& alloc = Allocator() ); |
(9) | (C++11およびそれ以降) |
template < class T > explicit basic_string( const T& t, const Allocator& alloc = Allocator() ); |
(10) | (C++17およびそれ以降) |
template < class T > basic_string( const T& t, size_type pos, size_type n, |
(11) | (C++17およびそれ以降) |
様々なデータソースと、オプショナルなユーザ供給のアロケータ alloc
から、新しい文字列を構築します。
1) デフォルトコンストラクタ。 空の文字列 (ゼロのサイズと未規定の容量) を構築します。 アロケータが供給されなければ、アロケータはデフォルト構築されたインスタンスから取得されます。
2) 文字
ch
のコピーを count
個持つ文字列を構築します。 count >= npos
の場合、動作は未定義です。3)
other
の部分文字列 [pos, pos+count)
を持つ文字列を構築します。 count == npos の場合、 count
が指定されない場合、または要求された部分文字列が文字列の終端を超える場合、結果の部分文字列は [pos, size())
になります。4)
s
の指す文字列の最初の count
個の文字を持つ文字列を構築します。 s
はNULL文字を含むこともできます。 文字列の長さは count
です。 s
が CharT
の要素を少なくとも count
個持つ配列を指さない場合 (s
がNULLポインタの場合も含みます)、動作は未定義です。5)
s
の指すNULL終端文字列のコピーで初期化された内容を持つ文字列を構築します。 文字列の長さは最初のNULL文字によって決まります。 s
が CharT
の要素を少なくとも Traits::length(s)+1 個持つ配列を指さない場合 (s
がNULLポインタの場合も含みます)、動作は未定義です。6) 範囲
[first, last)
の内容を持つ文字列を構築します。 InputIt
が整数型の場合は basic_string(static_cast<size_type>(first), static_cast<value_type>(last), a) と同等です。7) コピーコンストラクタ。
other
の内容のコピーを持つ文字列を構築します。8) ムーブコンストラクタ。 ムーブセマンティクスを用いて
other
の内容を持つ文字列を構築します。 other
は有効だけれども未規定な状態になります。9) 初期化子リスト
init
の内容を持つ文字列を構築します。10) std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、
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 である場合にのみ、オーバーロード解決に参加します。11) std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、
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 | - | 文字列を初期化するためのソースとして使用する別の文字列 |
init | - | 文字列を初期化するための std::initializer_list |
t | - | 文字列を初期化するための (std::basic_string_view に変換可能な) オブジェクト |
[編集] 計算量
1) 一定。
2-4)
count
に比例。5)
s
の長さに比例。6)
first
と last
の距離に比例。7)
other
のサイズに比例。8) 一定。
alloc
が指定されていて alloc != other.get_allocator() の場合は比例。9)
init
のサイズに比例。[編集] 例外
8) alloc == str.get_allocator() の場合は何も投げません。
Allocator::allocate
の呼び出しが例外を投げるかもしれません。
[編集] ノート
途中に '\0' を持つ文字列リテラルの初期化はオーバーロード (5) を使用し、最初のNULL文字で停止します。 これは別のコンストラクタを指定するか、 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 |
[編集] 例
Run this code
#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
[編集] 関連項目
文字列に文字を代入します (パブリックメンバ関数) | |
文字列に値を代入します (パブリックメンバ関数) |