std::string operator=
- since C++23
- since C++20
- since C++17
- since C++11
- until C++11
// (1) Non const version only
constexpr basic_string& operator=( const basic_string& str );
// (2) Non const version only
constexpr basic_string& operator=( basic_string&& str ) noexcept(/* see below */);
// (3) Non const version only
constexpr basic_string& operator=( const CharT* s );
// (4) Non const version only
constexpr basic_string& operator=( CharT ch );
// (5) Non const version only
constexpr basic_string& operator=( std::initializer_list<CharT> ilist );
// (6) Non const version only
template<class StringViewLike>
constexpr basic_string& operator=( const StringViewLike& t );
// (7) Non const version only
constexpr basic_string& operator=( std::nullptr_t ) = delete;
// (1) Non const version only
constexpr basic_string& operator=( const basic_string& str );
// (2) Non const version only
constexpr basic_string& operator=( basic_string&& str ) noexcept(/* see below */);
// (3) Non const version only
constexpr basic_string& operator=( const CharT* s );
// (4) Non const version only
constexpr basic_string& operator=( CharT ch );
// (5) Non const version only
constexpr basic_string& operator=( std::initializer_list<CharT> ilist );
// (6) Non const version only
template<class StringViewLike>
constexpr basic_string& operator=( const StringViewLike& t );
// (1) Non const version only
basic_string& operator=( const basic_string& str );
// (2) Non const version only
basic_string& operator=( basic_string&& str ) noexcept(/* see below */);
// (3) Non const version only
basic_string& operator=( const CharT* s );
// (4) Non const version only
basic_string& operator=( CharT ch );
// (5) Non const version only
basic_string& operator=( std::initializer_list<CharT> ilist );
// (6) Non const version only
template<class StringViewLike>
basic_string& operator=( const StringViewLike& t );
// (1) Non const version only
basic_string& operator=( const basic_string& str );
// (2) Non const version only
basic_string& operator=( basic_string&& str );
// (3) Non const version only
basic_string& operator=( const CharT* s );
// (4) Non const version only
basic_string& operator=( CharT ch );
// (5) Non const version only
basic_string& operator=( std::initializer_list<CharT> ilist );
// (1) Non const version only
basic_string& operator=( const basic_string& str );
// (3) Non const version only
basic_string& operator=( const CharT* s );
// (4) Non const version only
basic_string& operator=( CharT ch );
Replaces the contents of the string.
-
(1) Copy assignment operator. Replaces the contents with a copy of the contents of
other.noteIf
*thisandstrare the same object, this function has no effect. -
(2) Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container).
importantotheris in a valid but unspecified state afterwards.If
std::allocator_traits<Allocator>::propagate_on_container_move_assignment::valueistrue, the allocator of*thisis replaced by a copy of that ofstr.If it is
falseand the allocators of*thisandstrdo not compare equal,*thiscannot take ownership of the memory owned bystrand must assign each character individually, allocating additional memory using its own allocator as needed.InvalidationUnlike other container move assignments, references, pointers, and iterators to
strmay be invalidated. -
(3) Replaces the contents with those of null-terminated character string pointed to by
sas if byassign(s, Traits::length(s)). -
(4) Replaces the contents with character
chas if byassign(std::addressof(ch), 1). -
(5) Replaces the contents with those of the initializer list
ilistas if byassign(ilist.begin(), ilist.size()). -
(6) Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those of thesvas if byassign(sv).Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrueandstd::is_convertible_v<const StringViewLike&, const CharT*>isfalse. -
(7) Deleted constructor for
std::nullptr_tmakes it so thatstd::stringcannot be assigned fromnullptr.
Parameters
ch- value to initialize characters of the string withstr- string to be used as source to initialize the string withs- pointer to a null-terminated character string to use as source to initialize the string withilist-std::initializer_listto initialize the string witht- object convertible tostd::basic_string_viewto initialize the string with
Return value
*this
Complexity
- (1) Linear in the size of
str- O(str.size()). - (2)
Linear in the size of*this- O(size()).
If allocators do not compare equal and do not propagate, then also linear in the size ofstr- O(size() + str.size()). - (3) Linear in the size of
s- O(s.size()). - (4) Constant - O(1).
- (5) Linear in size of
ilist- O(ilist.size()).
Exceptions
- since C++17
- (2) noexcept specification:
noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value
|| std::allocator_traits<Allocator>::is_always_equal::value)
If the operation would result in size() > max_size(), throws std::length_error.
Example
#include <string>
#include <iostream>
#include <iomanip>
int main()
{
std::string str1;
std::string str2 { "alpha" };
// (1) operator=( const basic_string& );
str1 = str2;
std::cout << std::quoted(str1) << ' ' // "alpha"
<< std::quoted(str2) << '\n'; // "alpha"
// (2) operator=( basic_string&& );
str1 = std::move(str2);
std::cout << std::quoted(str1) << ' ' // "alpha"
<< std::quoted(str2) << '\n'; // "" or "alpha" (unspecified)
// (3) operator=( const CharT* );
str1 = "beta";
std::cout << std::quoted(str1) << '\n'; // "beta"
// (4) operator=( CharT );
str1 = '!';
std::cout << std::quoted(str1) << '\n'; // "!"
// (5) operator=( std::initializer_list<CharT> );
str1 = {'g','a','m','m','a'};
std::cout << std::quoted(str1) << '\n'; // "gamma"
// (6) operator=( const T& );
str1 = 35U; // equivalent to str1 = static_cast<char>(35U);
std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35)
}
"alpha" "alpha"
"alpha" ""
"beta"
"!"
"gamma"
"#"
Hover to see the original license.