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+=( CharT ch);
      (2)(constexpr since C++20)
      basic_string& operator+=(const CharT* s);
      (3)(constexpr since C++20)
      basic_string& operator+=(std::initializer_list<CharT> ilist);
      (4)(since C++11)
      (constexpr since C++20)
      template<class StringViewLike>
      basic_string& operator+=(const StringViewLike& t);
      (5)(since C++17)
      (constexpr since C++20)

      Appends additional characters to the string.

      1) Appends stringstr.
      2) Appends characterch.
      3) Appends the null-terminated character string pointed to bys.
      4) Appends characters in thestd::initializer_listilist.
      5) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then appends characters in the string viewsv as if byappend(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.

      Contents

      [edit]Parameters

      str - string to append
      ch - character value to append
      s - pointer to a null-terminated character string to append
      ilist -std::initializer_list with the characters to append
      t - object (convertible tostd::basic_string_view) with the characters to append

      [edit]Return value

      *this

      [edit]Complexity

      There are no standard complexity guarantees, typical implementations behave similar tostd::vector::insert().

      [edit]Exceptions

      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]Notes

      Overload(2) can accept any types that are implicitly convertible toCharT. Forstd::string, whereCharT ischar, the set of acceptable types includes all arithmetic types. This may have unintended effects.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <string> int main(){std::string str; // reserve sufficient storage space to avoid memory reallocation    str.reserve(50); std::cout<<std::quoted(str)<<'\n';// empty string     str+="This";std::cout<<std::quoted(str)<<'\n';     str+=std::string(" is ");std::cout<<std::quoted(str)<<'\n';     str+='a';std::cout<<std::quoted(str)<<'\n';     str+={' ','s','t','r','i','n','g','.'};std::cout<<std::quoted(str)<<'\n';     str+=69.96;// Equivalent to str += static_cast<char>(69.96);// 'E' (ASCII code 69) is appended by overload (2),// which might not be the intent. // To add a numeric value, consider std::to_string():    str+=std::to_string(1729); std::cout<<std::quoted(str)<<'\n';}

      Output:

      """This""This is ""This is a""This is a string.""This is a string.E1729"

      [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 2946C++17overload(5) caused ambiguity in some casesavoided by making it a template

      [edit]See also

      appends characters to the end
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator%2B%3D&oldid=183387"

      [8]ページ先頭

      ©2009-2025 Movatter.jp