std::basic_basic_string<CharT,Traits,Allocator>::assign
From cppreference.com
< cpp | string | basic string
basic_string& assign( const basic_string& str ); |
(1) | (C++20 以降 constexpr) |
basic_string& assign( basic_string&& str ) noexcept(/* 参照 */); |
(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, |
(7) | (C++17以降) (C++20 以降 constexpr) |
| (8) | ||
| basic_string& assign( const basic_string& str, size_type pos, size_type count ); |
(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) 内容を文字 ch の count 回のコピーで置き換えます。
clear(); resize(n, c); return *this; と同等です。
4) 内容を範囲
[s, s + count) の文字のコピーで置き換えます。 範囲
[s, s + count)が有効な範囲でない場合、動作は未定義です。5) return assign(s, Traits::length(s)); と同等です。
6,7) 文字列ビュー sv から構築された t の文字で内容を置き換えます。
これらのオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。
- 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()); と同等です。
return assign(sv.data(), sv.size()); と同等です。
7) std::basic_string_view<CharT, Traits> sv = t;
return assign(sv.substr(pos, count)); と同等です。
return assign(sv.substr(pos, count)); と同等です。
8) str の文字で内容を置き換えます。
|
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 | - | 結果の文字列のサイズ |
| 文字 | - | 文字列の初期化に使用する文字の値 |
| 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を送出します。
何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。
[編集] 例
このコードを実行
#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) は スワップによって実装できるとされていました。 |
ムーブ代入が必要なように修正されました。 |
| LWG 2250 | C++98 | オーバーロード (8) の動作は pos > str.size() が true の場合、未定義でした。 |
この場合、常に例外をスローします。 |
| LWG 2579 | C++98 | オーバーロード (1) とコピー代入 演算子には異なる効果がありました。 |
それらは同じ効果を持つようになりました。 |
| LWG 2946 | C++17 | オーバーロード (6) は、一部のケースで曖昧さを引き起こしました。 | テンプレートにすることで回避されました。 |
[編集] 関連項目
| (C++23) |
文字列への文字範囲の代入 (public member function) |
basic_string を構築する(public member function) | |
| 文字列に値を代入する (public member function) |