basic_string& operator=( const basic_string& str );
basic_string& operator=( basic_string&& str );
| (since C++11)
basic_string& operator=( basic_string&& str ) noexcept(/* see below */);
basic_string& operator=( const CharT* s );
basic_string& operator=( CharT ch );
basic_string& operator=( std::initializer_list<CharT> ilist );
basic_string& operator=( const T& t );
Replaces the contents of the string.
strare the same object, this function has no effect.
strusing move semantics.
stris in a valid but unspecified state afterwards. If std::allocator_traits<Allocator>::propagate_on_container_move_assignment() is true, the target allocator is replaced by a copy of the source allocator. If it is false and the source and the target allocators do not compare equal, the target cannot take ownership of the source memory and must assign each character individually, allocating additional memory using its own allocator as needed. Unlike other container move assignments, references, pointers, and iterators to
strmay be invalidated.
sas if by assign(s, Traits::length(s)).
chas if by assign(std::addressof(ch), 1)
ilistas if by assign(ilist.begin(), ilist.size())
tto a string view
svas if by std::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those of the
svas if by assign(sv). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const T&, const CharT*> is false.
|ch||-||value to initialize characters of the string with|
|str||-||string to be used as source to initialize the string with|
|s||-||pointer to a null-terminated character string to use as source to initialize the string with|
|ilist||-||std::initializer_list to initialize the string with|
|t||-||object convertible to std::basic_string_view to initialize the string with|
this(formally, each CharT has to be destroyed). If allocators do not compare equal and do not propagate, then also linear in the size of
str(copy must be made)
If the operation would result in
size() > max_size(), throws std::length_error.
If an exception is thrown for any reason, this function has no effect (strong exception guarantee).
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
|DR||Applied to||Behavior as published||Correct behavior|
|LWG 2063||C++11||non-normative note stated that swap is a valid implementation of move assignment||corrected to support allocators|
||avoided by making it a template|
|This section is incomplete|
Reason: no example
| constructs a |
(public member function)
| assign characters to a string |
(public member function)