Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& insert( size_type index, size_type count, CharT ch);
      (1)(constexpr since C++20)
      basic_string& insert( size_type index,const CharT* s);
      (2)(constexpr since C++20)
      basic_string& insert( size_type index,const CharT* s, size_type count);
      (3)(constexpr since C++20)
      basic_string& insert( size_type index,const basic_string& str);
      (4)(constexpr since C++20)
      (5)
      basic_string& insert( size_type index,const basic_string& str,
                            size_type s_index, size_type count);
      (until C++14)
      basic_string& insert( size_type index,const basic_string& str,
                            size_type s_index, size_type count= npos);
      (since C++14)
      (constexpr since C++20)
      (6)
      iterator insert( iterator pos, CharT ch);
      (until C++11)
      iterator insert( const_iterator pos, CharT ch);
      (since C++11)
      (constexpr since C++20)
      (7)
      void insert( iterator pos, size_type count, CharT ch);
      (until C++11)
      iterator insert( const_iterator pos, size_type count, CharT ch);
      (since C++11)
      (constexpr since C++20)
      (8)
      template<class InputIt>
      void insert( iterator pos, InputIt first, InputIt last);
      (until C++11)
      template<class InputIt>
      iterator insert( const_iterator pos, InputIt first, InputIt last);
      (since C++11)
      (constexpr since C++20)
      iterator insert( const_iterator pos,std::initializer_list<CharT> ilist);
      (9)(since C++11)
      (constexpr since C++20)
      template<class StringViewLike>
      basic_string& insert( size_type index,const StringViewLike& t);
      (10)(since C++17)
      (constexpr since C++20)
      template<class StringViewLike>

      basic_string& insert( size_type index,const StringViewLike& t,

                            size_type t_index, size_type count= npos);
      (11)(since C++17)
      (constexpr since C++20)

      Inserts characters into the string.

      1) Insertscount copies of characterch at the positionindex.
      2) Inserts null-terminated character string pointed to bys at the positionindex. The length of the string is determined by the first null character usingTraits::length(s).
      3) Inserts the characters in the range[ss+ count) at the positionindex. The range can contain null characters.
      4) Inserts stringstr at the positionindex.
      5) Inserts a string, obtained bystr.substr(s_index, count) at the positionindex.
      6) Inserts characterch before the character pointed bypos.
      7) Insertscount copies of characterch before the element (if any) pointed bypos.
      8) Inserts characters from the range[firstlast) before the element (if any) pointed bypos, as if byinsert(pos- begin(), basic_string(first, last, get_allocator())).

      This overload does not participate in overload resolution ifInputIt does not satisfyLegacyInputIterator.

      (since C++11)
      9) Inserts elements from initializer listilist before the element (if any) pointed bypos.
      10) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then inserts the elements fromsv before the element (if any) pointed byindex, as if byinsert(index, sv.data(), sv.size()).
      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.
      11) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then inserts, before the element (if any) pointed byindex, the characters from the subview[t_indext_index+ count) ofsv.
      • If the requested subview lasts past the end ofsv, or ifcount== npos, the resulting subview is[t_indexsv.size()).
      • Ift_index> sv.size(), or ifindex> size(),std::out_of_range is thrown.
      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.

      Ifpos is not a valid iterator on*this, the behavior is undefined.

      Contents

      [edit]Parameters

      index - position at which the content will be inserted
      pos - iterator before which the characters will be inserted
      ch - character to insert
      count - number of characters to insert
      s - pointer to the character string to insert
      str - string to insert
      first, last - range defining characters to insert
      s_index - position of the first character instr to insert
      ilist -std::initializer_list to insert the characters from
      t - object (convertible tostd::basic_string_view) to insert the characters from
      t_index - position of the first character int to insert
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.

      [edit]Return value

      1-5)*this
      6-9) An iterator which refers to the copy of the first inserted character orpos if no characters were inserted (count==0 orfirst== last orilist.size()==0)
      10,11)*this

      [edit]Exceptions

      1-4,10) Throwsstd::out_of_range ifindex> size().
      5) Throwsstd::out_of_range ifindex> size() or ifs_index> str.size().
      11) Throwsstd::out_of_range ifindex> size() or ift_index> sv.size().

      In all cases, throwsstd::length_error ifsize()+ ins_count> max_size() whereins_count is the number of characters that will be inserted.

      In all cases, ifstd::allocator_traits<Allocator>::allocate throws an exception, it is rethrown.

      (since C++20)

      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 <iterator>#include <string> usingnamespace std::string_literals; int main(){std::string s="xmplr"; // insert(size_type index, size_type count, char ch)    s.insert(0,1,'E');assert("Exmplr"== s); // insert(size_type index, const char* s)    s.insert(2,"e");assert("Exemplr"== s); // insert(size_type index, string const& str)    s.insert(6,"a"s);assert("Exemplar"== s); // insert(size_type index, string const& str,//        size_type s_index, size_type count)    s.insert(8," is an example string."s,0,14);assert("Exemplar is an example"== s); // insert(const_iterator pos, char ch)    s.insert(s.cbegin()+ s.find_first_of('n')+1,':');assert("Exemplar is an: example"== s); // insert(const_iterator pos, size_type count, char ch)    s.insert(s.cbegin()+ s.find_first_of(':')+1,2,'=');assert("Exemplar is an:== example"== s); // insert(const_iterator pos, InputIt first, InputIt last){std::string seq=" string";        s.insert(s.begin()+ s.find_last_of('e')+1,std::begin(seq),std::end(seq));assert("Exemplar is an:== example string"== s);} // insert(const_iterator pos, std::initializer_list<char>)    s.insert(s.cbegin()+ s.find_first_of('g')+1,{'.'});assert("Exemplar is an:== example string."== s);}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 7C++98overload(8) referred to a non-existing overloadrefers to overload(4) correctly
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 2946C++17overload(10) caused ambiguity in some casesavoided by making it a template

      [edit]See also

      inserts a range of characters
      (public member function)[edit]
      appends characters to the end
      (public member function)[edit]
      appends a character to the end
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/insert&oldid=171131"

      [8]ページ先頭

      ©2009-2025 Movatter.jp