Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::forward_list<T,Allocator>::insert_after

      From cppreference.com
      <cpp‎ |container‎ |forward list
       
       
       
      std::forward_list
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      iterator insert_after( const_iterator pos,const T& value);
      (1)(since C++11)
      (constexpr since C++26)
      iterator insert_after( const_iterator pos, T&& value);
      (2)(since C++11)
      (constexpr since C++26)
      iterator insert_after( const_iterator pos,
                             size_type count,const T& value);
      (3)(since C++11)
      (constexpr since C++26)
      template<class InputIt>

      iterator insert_after( const_iterator pos,

                             InputIt first, InputIt last);
      (4)(since C++11)
      (constexpr since C++26)
      iterator insert_after( const_iterator pos,
                             std::initializer_list<T> ilist);
      (5)(since C++11)
      (constexpr since C++26)

      Inserts elements after the specified position in the container. Ifpos isbefore_begin(), the first element inserted (if exists) will become the first element of*this.

      Ifpos is not in the range[before_begin()end()), the behavior is undefined.

      1,2) Inserts a copy ofvalue afterpos.
      1) IfT is notCopyInsertable intoforward_list, the behavior is undefined.
      2) IfT is notMoveInsertable intoforward_list, the behavior is undefined.
      3) Insertscount copies of thevalue afterpos.
      IfT is notCopyInsertable intoforward_list, the behavior is undefined.
      4) Inserts elements from range[firstlast) afterpos.
      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.
      If any of the following conditions is satisfied, the behavior is undefined:
      5) Inserts elements from initializer listilist afterpos.
      Equivalent toreturn insert_after(position, ilist.begin(), ilist.end());.

      No iterators or references are invalidated.

      Contents

      [edit]Parameters

      pos - iterator after which the content will be inserted
      value - element value to insert
      count - number of copies to insert
      first, last - the pair of iterators defining the sourcerange of elements to insert
      ilist - initializer list to insert the values from

      [edit]Return value

      1,2) Iterator to the inserted element.
      3) Iterator to the last element inserted, orpos ifcount==0 istrue.
      4) Iterator to the last element inserted, orpos iffirst== last istrue.
      5) Iterator to the last element inserted, orpos ifilist is empty.

      [edit]Exceptions

      If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).

      [edit]Complexity

      1,2) Constant.
      3) Linear incount.
      4) Linear instd::distance(first, last).
      5) Linear inilist.size().

      [edit]Example

      Run this code
      #include <forward_list>#include <iostream>#include <string>#include <vector> void print(conststd::forward_list<int>& list){std::cout<<"list: {";for(char comma[3]={'\0',' ','\0'};int i: list){std::cout<< comma<< i;        comma[0]=',';}std::cout<<"}\n";} int main(){std::forward_list<int> ints{1,2,3,4,5};    print(ints); // insert_after (2)auto beginIt= ints.begin();    ints.insert_after(beginIt,-6);    print(ints); // insert_after (3)auto anotherIt= beginIt;++anotherIt;    anotherIt= ints.insert_after(anotherIt,2,-7);    print(ints); // insert_after (4)conststd::vector<int> v={-8,-9,-10};    anotherIt= ints.insert_after(anotherIt, v.cbegin(), v.cend());    print(ints); // insert_after (5)    ints.insert_after(anotherIt,{-11,-12,-13,-14});    print(ints);}

      Output:

      list: {1, 2, 3, 4, 5}list: {1, -6, 2, 3, 4, 5}list: {1, -6, -7, -7, 2, 3, 4, 5}list: {1, -6, -7, -7, -8, -9, -10, 2, 3, 4, 5}list: {1, -6, -7, -7, -8, -9, -10, -11, -12, -13, -14, 2, 3, 4, 5}

      [edit]See also

      constructs elements in-place after an element
      (public member function)[edit]
      inserts an element to the beginning
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/forward_list/insert_after&oldid=183063"

      [8]ページ先頭

      ©2009-2025 Movatter.jp