std::basic_string<CharT,Traits,Allocator>:: assign
|
basic_string
&
assign
(
const
basic_string
&
str
)
;
|
(1) | (constexpr since C++20) |
|
basic_string
&
assign
(
basic_string
&&
str
)
noexcept
(
/* see below */
)
;
|
(2) |
(since C++11)
(constexpr since C++20) |
|
basic_string
&
assign
(
size_type count, CharT ch
)
;
|
(3) | (constexpr since C++20) |
|
basic_string
&
assign
(
const
CharT
*
s, size_type count
)
;
|
(4) | (constexpr since C++20) |
|
basic_string
&
assign
(
const
CharT
*
s
)
;
|
(5) | (constexpr since C++20) |
|
template
<
class
SV
>
basic_string & assign ( const SV & t ) ; |
(6) |
(since C++17)
(constexpr since C++20) |
|
template
<
class
SV
>
basic_string
&
assign
(
const
SV
&
t,
|
(7) |
(since C++17)
(constexpr since C++20) |
| (8) | ||
|
basic_string
&
assign
(
const
basic_string
&
str,
size_type pos, size_type count ) ; |
(until C++14) | |
|
basic_string
&
assign
(
const
basic_string
&
str,
size_type pos, size_type count = npos ) ; |
(since C++14)
(constexpr since C++20) |
|
|
template
<
class
InputIt
>
basic_string & assign ( InputIt first, InputIt last ) ; |
(9) | (constexpr since C++20) |
|
basic_string
&
assign
(
std::
initializer_list
<
CharT
>
ilist
)
;
|
(10) |
(since C++11)
(constexpr since C++20) |
文字列の内容を置き換えます。
[
s
,
s
+
count
)
内の文字のコピーで置き換えます。
- std:: is_convertible_v < const SV & , std:: basic_string_view < CharT, Traits >> が true であること。
- std:: is_convertible_v < const SV & , const CharT * > が false であること。
return assign ( sv. data ( ) , sv. size ( ) ) ; 。
return assign ( sv. substr ( pos, count ) ) ; .
|
次と等価:
return
assign
(
std::
basic_string_view
<
CharT, Traits
>
( str ) . substr ( pos, count ) ) ; . |
(C++20以降) |
|
このオーバーロードは、
|
(C++11以降) |
目次 |
パラメータ
| str | - | 文字列の初期化に使用するソース文字列 |
| count | - | 生成される文字列のサイズ |
| ch | - | 文字列の文字を初期化するための値 |
| s | - | 文字列の初期化に使用するソース文字列へのポインタ |
| t | - | 文字列の文字を初期化するためのオブジェクト( std::basic_string_view に変換可能) |
| pos | - | 取得する最初の文字のインデックス |
| first, last | - | 文字をコピーする範囲 |
| ilist | - | 文字列の文字を初期化するための std::initializer_list |
戻り値
* this
例外
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
を構築する
(公開メンバ関数) |
|
|
文字列に値を代入する
(公開メンバ関数) |