std::basic_string<CharT,Traits,Allocator>:: basic_string
| (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,
|
(4) | (constexpr C++20以降) |
|
template
<
container-compatible-range
<
CharT
>
R
>
constexpr
basic_string
(
std::
from_range_t
, R
&&
rg,
|
(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,
|
(9) |
(C++17以降)
(C++20以降 constexpr) |
|
template
<
class
StringViewLike
>
basic_string
(
const
StringViewLike
&
t,
|
(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,
|
(17) | (constexpr since C++20) |
|
constexpr
basic_string
(
basic_string
&&
other,
size_type pos, size_type count,
|
(18) | (C++23以降) |
|
basic_string
(
std::
initializer_list
<
CharT
>
ilist,
const Allocator & alloc = Allocator ( ) ) ; |
(19) |
(C++11以降)
(C++20以降constexpr) |
様々なデータソースから新しい文字列を構築し、オプションでユーザー提供のアロケータ alloc を使用します。
|
|
(C++11以降) |
|
このオーバーロードは、
|
(C++17以降) |
[
first
,
last
)
の内容を持つ文字列を構築します。
[
first
,
last
)
内の各イテレータは正確に1回デリファレンスされます。
|
|
(C++11以前) |
|
このオーバーロードは、
|
(C++11以降) |
CharT
が
EmplaceConstructible
でない場合、
std::
basic_string
<
CharT
>
に
*
ranges::
begin
(
rg
)
から構築できない場合、動作は未定義です。
[
s
,
s
+
count
)
。
[
s
,
s
+
count
)
が有効な範囲でない場合、動作は未定義です。
|
このオーバーロードは、
|
(C++17以降) |
std::basic_string
は
nullptr
から構築できません。
std:: basic_string_view < CharT, Traits >> が true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > が false である場合です。
std:: basic_string_view < CharT, Traits >> が true である場合にのみ、オーバーロード解決に参加します。
basic_string&&
の場合、構築完了時には
other
は有効だが未規定の状態になります。
|
アロケータは、以下の呼び出しによって取得されるかのように取得される:
std::
allocator_traits
<
Allocator
>
::
|
(C++11以降) |
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
other.
size
(
)
)
.
[
other.
data
(
)
+
pos
,
other.
data
(
)
+
(
pos
+
std::
min
(
count, other.
size
(
)
-
pos
)
)
)
.
目次 |
パラメータ
| alloc | - | この文字列のすべてのメモリ割り当てに使用するアロケータ |
| count | - | 結果の文字列のサイズ |
| ch | - | 文字列の初期化に使用する値 |
| pos | - | 含める最初の文字の位置 |
| first, last | - | 文字をコピーする範囲 |
| s | - | 文字列の初期化に使用する文字配列へのポインタ |
| other | - | 文字列の初期化に使用する別の文字列 |
| ilist | - | std::initializer_list 文字列の初期化に使用する |
| t | - | 文字列の初期化に使用するオブジェクト( std::basic_string_view に変換可能) |
| rg | - | コンテナ互換の範囲 |
計算量
例外
構築される文字列の長さが
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] |
-
↑
[nullptr,nullptr + 0)は有効な空範囲です。なぜなら、nullポインタ値にゼロを加えることも 明確に定義されている からです(結果は依然としてnullポインタ値となります)。
関連項目
|
文字列に文字を割り当てる
(公開メンバ関数) |
|
|
文字列に値を代入する
(公開メンバ関数) |
|
|
(C++11)
|
整数値または浮動小数点値を
string
に変換する
(関数) |
|
(C++11)
|
整数値または浮動小数点値を
wstring
に変換する
(関数) |
basic_string_view
を構築する
(
std::basic_string_view<CharT,Traits>
の公開メンバ関数)
|