std::string operator=
- od C++23
- od C++20
- od C++17
- od C++11
- do 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
.zanotujIf
*this
andstr
are 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).
importantother
is in a valid but unspecified state afterwards.If
std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value
istrue
, the allocator of*this
is replaced by a copy of that ofstr
.If it is
false
and the allocators of*this
andstr
do not compare equal,*this
cannot take ownership of the memory owned bystr
and must assign each character individually, allocating additional memory using its own allocator as needed.InvalidationUnlike other container move assignments, references, pointers, and iterators to
str
may be invalidated. -
(3) Replaces the contents with those of null-terminated character string pointed to by
s
as if byassign(s, Traits::length(s))
. -
(4) Replaces the contents with character
ch
as if byassign(std::addressof(ch), 1)
. -
(5) Replaces the contents with those of the initializer list
ilist
as if byassign(ilist.begin(), ilist.size())
. -
(6) Implicitly converts
t
to a string viewsv
as if bystd::basic_string_view<CharT, Traits> sv = t;
, then replaces the contents with those of thesv
as if byassign(sv)
.Overload ResolutionThis overload participates in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
istrue
andstd::is_convertible_v<const StringViewLike&, const CharT*>
isfalse
. -
(7) Deleted constructor for
std::nullptr_t
makes it so thatstd::string
cannot 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_list
to initialize the string witht
- object convertible tostd::basic_string_view
to 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
- od 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.