Przejdź do głównej zawartości

std::string operator=

// (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;

Replaces the contents of the string.

  • (1) Copy assignment operator. Replaces the contents with a copy of the contents of other.

    zanotuj

    If *this and str 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).

    important

    other is in a valid but unspecified state afterwards.

    If std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value is true, the allocator of *this is replaced by a copy of that of str.

    If it is false and the allocators of *this and str do not compare equal, *this cannot take ownership of the memory owned by str and must assign each character individually, allocating additional memory using its own allocator as needed.

    Invalidation

    Unlike 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 by assign(s, Traits::length(s)).

  • (4) Replaces the contents with character ch as if by assign(std::addressof(ch), 1).

  • (5) Replaces the contents with those of the initializer list ilist as if by assign(ilist.begin(), ilist.size()).

  • (6) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those of the sv as if by assign(sv).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (7) Deleted constructor for std::nullptr_t makes it so that std::string cannot be assigned from nullptr.

Parameters

  • 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

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 of str - 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

  • (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.

If an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (od C++11)

Example

Main.cpp
#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)
}
Output
"alpha" "alpha"
"alpha" ""
"beta"
"!"
"gamma"
"#"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string operator=

// (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;

Replaces the contents of the string.

  • (1) Copy assignment operator. Replaces the contents with a copy of the contents of other.

    zanotuj

    If *this and str 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).

    important

    other is in a valid but unspecified state afterwards.

    If std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value is true, the allocator of *this is replaced by a copy of that of str.

    If it is false and the allocators of *this and str do not compare equal, *this cannot take ownership of the memory owned by str and must assign each character individually, allocating additional memory using its own allocator as needed.

    Invalidation

    Unlike 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 by assign(s, Traits::length(s)).

  • (4) Replaces the contents with character ch as if by assign(std::addressof(ch), 1).

  • (5) Replaces the contents with those of the initializer list ilist as if by assign(ilist.begin(), ilist.size()).

  • (6) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then replaces the contents with those of the sv as if by assign(sv).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (7) Deleted constructor for std::nullptr_t makes it so that std::string cannot be assigned from nullptr.

Parameters

  • 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

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 of str - 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

  • (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.

If an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (od C++11)

Example

Main.cpp
#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)
}
Output
"alpha" "alpha"
"alpha" ""
"beta"
"!"
"gamma"
"#"
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.