Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::append

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& append( size_type count, CharT ch);
      (1)(constexpr since C++20)
      basic_string& append(const CharT* s, size_type count);
      (2)(constexpr since C++20)
      basic_string& append(const CharT* s);
      (3)(constexpr since C++20)
      template<class SV>
      basic_string& append(const SV& t);
      (4)(since C++17)
      (constexpr since C++20)
      template<class SV>

      basic_string& append(const SV& t, size_type pos,

                            size_type count= npos);
      (5)(since C++17)
      (constexpr since C++20)
      basic_string& append(const basic_string& str);
      (6)(constexpr since C++20)
      (7)
      basic_string& append(const basic_string& str,
                            size_type pos, size_type count);
      (until C++14)
      basic_string& append(const basic_string& str,
                            size_type pos, size_type count= npos);
      (since C++14)
      (constexpr since C++20)
      template<class InputIt>
      basic_string& append( InputIt first, InputIt last);
      (8)(constexpr since C++20)
      basic_string& append(std::initializer_list<CharT> ilist);
      (9)(since C++11)
      (constexpr since C++20)

      Appends additional characters to the string.

      1) Appendscount copies of characterch.
      2) Appends characters in the range[ss+ count).
      If[ss+ count) is not avalid range, the behavior is undefined.
      3) Equivalent toreturn append(s, Traits::length(s));.
      4,5) Appends characters in a string viewsv constructed fromt.
      • If onlyt is provided, all characters insv are appended.
      • Ifpos is also provided:
        • Ifcount isnpos, all characters insv starting frompos are appended.
        • Otherwise, thestd::min(count, sv.size()- pos) characters insv starting frompos are appended.
      These overloads participate in overload resolution only if all following conditions are satisfied:
      4) Equivalent tostd::basic_string_view<CharT, Traits> sv= t;
      return append(sv.data(), sv.size());
      .
      5) Equivalent tostd::basic_string_view<CharT, Traits> sv= t;
      return append(sv.substr(pos, count));
      .
      6,7) Appends characters in another stringstr.
      • If onlystr is provided, all characters in it are appended.
      • Ifpos is also provided:
        • Ifcount isnpos, all characters instr starting frompos are appended.
        • Otherwise, thestd::min(count, str.size()- pos) characters instr starting frompos are appended.
      6) Equivalent toreturn append(str.data(), str.size());.
      7) Equivalent toreturn append(std::basic_string_view<CharT, Traits>
                       (str).substr(pos, count));
      .
      (since C++20)
      8) Equivalent toreturn append(basic_string(first, last, get_allocator()));.

      This overload has the same effect as overload(1) ifInputIt is an integral type.

      (until C++11)

      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.

      (since C++11)
      9) Equivalent toreturn append(ilist.begin(), ilist.size());.

      Contents

      [edit]Parameters

      count - number of characters to append
      ch - character value to append
      s - pointer to the character string to append
      t - object convertible tostd::basic_string_view with the characters to append
      pos - the index of the first character to append
      str - string to append
      first, last - range of characters to append
      ilist - initializer list 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.

      5) Ifpos> sv.size() istrue, throwsstd::out_of_range.
      7) Ifpos> str.size() istrue, throwsstd::out_of_range.

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

      [edit]Example

      Run this code
      #include <cassert>#include <string> int main(){std::string str="std::string";constchar* cptr="C-string";constchar carr[]="range"; std::string result; // 1) Append a char 3 times.// Note: This is the only overload accepting “CharT”s.    result.append(3,'*');assert(result=="***"); // 2) Append a fixed-length C-string    result.append(cptr,5);assert(result=="***C-str"); // 3) Append a null-terminated C-string// Note: Because “append” returns *this, we can chain calls together.    result.append(1,' ').append(cptr);assert(result=="***C-str C-string"); // 6) Append a whole string    result.append(1,' ').append(str);assert(result=="***C-str C-string std::string"); // 7) Append part of a string    result.append(str,3,2);assert(result=="***C-str C-string std::string::"); // 8) Append range    result.append(&carr[2],&carr[3]);assert(result=="***C-str C-string std::string::n"); // 9) Append initializer list    result.append({'p','o','s'});assert(result=="***C-str C-string std::string::npos");}

      [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 2250C++98the behavior of overload(7) was
      undefined ifpos> str.size() istrue
      always throws an exception in this case
      LWG 2788C++98overload(8) used a default constructed
      allocator to construct the temporary string
      obtains the allocator
      fromget_allocator()
      LWG 2946C++17overload(4) causes ambiguity in some casesavoided by making it a template

      [edit]See also

      appends a range of characters to the end
      (public member function)[edit]
      appends characters to the end
      (public member function)[edit]
      concatenates two strings
      (function)[edit]
      concatenates a certain amount of characters of two strings
      (function)[edit]
      appends a copy of one wide string to another
      (function)[edit]
      appends a certain amount of wide characters from one wide string to another
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/append&oldid=178401"

      [8]ページ先頭

      ©2009-2025 Movatter.jp