std::basic_string<CharT,Traits,Allocator>::assign
From ja.cppreference.net
basic_string& assign( const basic_string& str );
|
(1) | (C++20 から constexpr) |
basic_string& assign( basic_string&& str ) noexcept(/* see below */);
|
(2) | (C++11 以上) (C++20 から constexpr) |
basic_string& assign( size_type count, CharT ch );
|
(3) | (C++20 から constexpr) |
basic_string& assign( const CharT* s, size_type count );
|
(4) | (C++20 から constexpr) |
basic_string& assign( const CharT* s );
|
(5) | (C++20 から constexpr) |
template< class SV >
basic_string& assign( const SV& t );
|
(6) | (C++17 以上) (C++20 から constexpr) |
template< class SV >
basic_string& assign( const SV& t,
size_type pos, size_type count = npos);
|
(7) | (C++17 以上) (C++20 から constexpr) |
basic_string& assign( const basic_string& str,
size_type pos, size_type count );
|
(8) | (C++14 まで) |
basic_string& assign( const basic_string& str,
size_type pos, size_type count = npos);
|
(C++14 以上) (C++20 から constexpr) |
|
template< class InputIt >
basic_string& assign( InputIt first, InputIt last );
|
(9) | (C++20 から constexpr) |
basic_string& assign( std::initializer_list<CharT> ilist );
|
(10) | (C++11 以上) (C++20 から constexpr) |
文字列の内容を置き換えます。
1)
return *this = str; と同等です。2)
return *this = std::move(str); と同等です。3) 内容を文字
count の ch 個のコピーで置き換えます。clear(); resize(n, c); return *this; と同等です。4) 内容を範囲
[s, s + count) 内の文字のコピーで置き換えます。[s, s + count) が 有効な範囲 でない場合、動作は未定義です。5)
return assign(s, Traits::length(s)); と同等です。6,7) 内容を、
sv から構築された文字列ビュー t 内の文字で置き換えます。
tのみが指定された場合、内容をsv内のすべての文字で置き換えます。posも指定された場合:countがnposの場合、内容をsv内のposから始まるすべての文字で置き換えます。- そうでない場合、内容を
std::min(count, sv.size() - pos)内のsvから始まるpos個の文字で置き換えます。
これらのオーバーロードは、以下のすべての条件が満たされる場合にのみオーバーロード解決に参加します:
std::is_convertible_v<const SV&, std::basic_string_view<CharT, Traits>>はtrueである。std::is_convertible_v<const SV&, const CharT*>はfalseである。
6)
std::basic_string_view<CharT, Traits> sv = t;return assign(sv.data(), sv.size()); と同等です。7)
std::basic_string_view<CharT, Traits> sv = t;return assign(sv.substr(pos, count)); と同等です。8) 内容を
str 内の文字で置き換えます。
countがnposの場合、内容をstr内のposから始まるすべての文字で置き換えます。- そうでない場合、内容を
std::min(count, str.size() - pos)内のstrから始まるpos個の文字で置き換えます。
return assign(std::basic_string_view<CharT, Traits>(str).substr(pos, count)); と同等です。 |
(C++20 以上) |
9)
return assign(basic_string(first, last, get_allocator())); と同等です。
|
このオーバーロードは、 |
(C++11 以上) |
10)
return assign(ilist.begin(), ilist.size()); と同等です。パラメータ
| str | - | 文字列の初期化に使用するソース文字列 |
| count | - | 生成される文字列のサイズ |
| ch | - | 文字列の文字を初期化するための値 |
| s | - | 文字列の初期化に使用するソース文字列へのポインタ |
| t | - | 文字列の文字を初期化するためのオブジェクト( std::basic_string_view に変換可能) |
| pos | - | 取得する最初の文字のインデックス |
| first, last | - | 文字をコピーする範囲 |
| ilist | - | 文字列の文字を初期化するための std::initializer_list |
戻り値
* this
例外
2)
noexcept
指定:
noexcept
(
std::
allocator_traits
<
Allocator
>
::
propagate_on_container_move_assignment
::
value
||
操作によって
size()
が
max_size()
を超える場合、
std::length_error
をスローします。
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
例
このコードを実行
#include <iostream> #include <iterator> #include <string> int main() { std::string s; // assign(size_type count, CharT ch) s.assign(4, '='); std::cout << s << '\n'; // "====" std::string const c("Exemplary"); // assign(const basic_string& str) s.assign(c); std::cout << c << " == " << s << '\n'; // "Exemplary == Exemplary" // assign(const basic_string& str, size_type pos, size_type count) s.assign(c, 0, c.length() - 1); std::cout << s << '\n'; // "Exemplar"; // assign(basic_string&& str) s.assign(std::string("C++ by ") + "example"); std::cout << s << '\n'; // "C++ by example" // assign(const CharT* s, size_type count) s.assign("C-style string", 7); std::cout << s << '\n'; // "C-style" // assign(const CharT* s) s.assign("C-style\0string"); std::cout << s << '\n'; // "C-style" char mutable_c_str[] = "C-style string"; // assign(InputIt first, InputIt last) s.assign(std::begin(mutable_c_str), std::end(mutable_c_str) - 1); std::cout << s << '\n'; // "C-style string" // assign(std::initializer_list<CharT> ilist) s.assign({'C', '-', 's', 't', 'y', 'l', 'e'}); std::cout << s << '\n'; // "C-style" }
出力:
==== Exemplary == Exemplary Exemplar C++ by example C-style C-style C-style string C-style
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用バージョン | 公開時の動作 | 修正後の動作 |
|---|---|---|---|
| LWG 847 | C++98 | 例外安全性保証が存在しなかった | 強い例外安全性保証を追加 |
| LWG 2063 | C++11 |
非規範的な注記でオーバーロード
(
2
)
がswapで実装可能と記述されていた |
ムーブ代入を要求するように修正 |
| LWG 2250 | C++98 |
オーバーロード
(
8
)
の動作が
pos > str. size ( ) の場合 true だと未定義 |
この場合常に例外をスロー |
| LWG 2579 | C++98 |
オーバーロード
(
1
)
とコピー代入演算子の 効果が異なっていた |
同じ効果を持つように修正 |
| LWG 2946 | C++17 | オーバーロード ( 6 ) が一部の場合で曖昧性を生じた | テンプレート化することで回避 |
関連項目
|
(C++23)
|
文字の範囲を文字列に割り当てる
(公開メンバ関数) |
basic_string
を構築する
(公開メンバ関数) |
|
|
文字列に値を代入する
(公開メンバ関数) |