Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::operator=

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& operator=(const basic_string& str);
      (1)(constexpr since C++20)
      basic_string& operator=( basic_string&& str)
         noexcept(/* see below */);
      (2)(since C++11)
      (constexpr since C++20)
      basic_string& operator=(const CharT* s);
      (3)(constexpr since C++20)
      basic_string& operator=( CharT ch);
      (4)(constexpr since C++20)
      basic_string& operator=(std::initializer_list<CharT> ilist);
      (5)(since C++11)
      (constexpr since C++20)
      template<class StringViewLike>
      basic_string& operator=(const StringViewLike& t);
      (6)(since C++17)
      (constexpr since C++20)
      basic_string& operator=(std::nullptr_t)= delete;
      (7)(since C++23)

      Replaces the contents of the string.

      1) Replaces the contents with a copy ofstr. If*this andstr are the same object, this function has no effect.
      2) Replaces the contents with those ofstr usingSequenceContainer's move assignment semantics.
      Unlike other sequence container move assignments, references, pointers, and iterators to elements ofstr may be invalidated.
      3) Replaces the contents with those of null-terminated character string pointed to bys as if byassign(s, Traits::length(s)).
      4) Replaces the contents with characterch as if byassign(std::addressof(ch),1).
      5) Replaces the contents with those of the initializer listilist as if byassign(ilist.begin(), ilist.size()).
      6) Implicitly convertst 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).
      This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&,
                           std::basic_string_view<CharT, Traits>>
      istrue andstd::is_convertible_v<const StringViewLike&,const CharT*> isfalse.
      7)std::basic_string cannot be assigned fromnullptr.

      Contents

      [edit]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 tostd::basic_string_view to initialize the string with

      [edit]Return value

      *this

      [edit]Complexity

      1) Linear in size ofstr.
      2) Linear in the size of*this (formally, eachCharT has to be destroyed). If allocators do not compare equal and do not propagate, then also linear in the size ofstr (copy must be made).
      3) Linear in size ofs.
      4) Constant.
      5) Linear in size ofilist.
      6) Linear in size oft.

      [edit]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 causesize() to exceedmax_size(), throwsstd::length_error.

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <string> 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)}

      Possible output:

      "alpha" "alpha""alpha" """beta""!""gamma""#"

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 2063C++11the move assignment operator did not follow
      SequenceContainer's semantic requirement
      follows
      LWG 2946C++17overload(6) caused ambiguity in some casesavoided by making it a template

      [edit]See also

      constructs abasic_string
      (public member function)[edit]
      assign characters to a string
      (public member function)[edit]
      assigns a view
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator%3D&oldid=172092"

      [8]ページ先頭

      ©2009-2025 Movatter.jp