Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::list<T,Allocator>::splice

      From cppreference.com
      <cpp‎ |container‎ |list
       
       
       
      std::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)
       
      void splice( const_iterator pos, list& other);
      (1)(constexpr since C++26)
      void splice( const_iterator pos, list&& other);
      (2)(since C++11)
      (constexpr since C++26)
      void splice( const_iterator pos, list& other, const_iterator it);
      (3)(constexpr since C++26)
      void splice( const_iterator pos, list&& other, const_iterator it);
      (4)(since C++11)
      (constexpr since C++26)
      void splice( const_iterator pos, list& other,
                   const_iterator first, const_iterator last);
      (5)(constexpr since C++26)
      void splice( const_iterator pos, list&& other,
                   const_iterator first, const_iterator last);
      (6)(since C++11)
      (constexpr since C++26)

      Transfers elements fromother to*this. The elements are inserted atpos.

      If any of the following conditions is satisfied, the behavior is undefined:

      • pos is not in the range[begin()end()).
      • get_allocator()== other.get_allocator() isfalse.
      1,2) Transfers all elements ofother.other becomes empty after the operation.
      If*this andother refer to the same object, the behavior is undefined.
      3,4) Transfers the element pointed to byit.
      *this andother can refer to the same object. In this case, there is no effect ifpos== it orpos==++it istrue.
      Ifit is not in the range[begin()end()), the behavior is undefined.
      5,6) Transfers elements in the range[firstlast).
      *this andother can refer to the same object.
      If any of the following conditions is satisfied, the behavior is undefined:
      • [firstlast) is not avalid range inother,
      • Any iterator in[firstlast) is not dereferenceable.
      • pos is in[firstlast).

      No iterators or references become invalidated. If*this andother refer to different objects, the iterators to the transferred elements now refer into*this, not intoother.

      Contents

      [edit]Parameters

      pos - element before which the content will be inserted
      other - another container to transfer the content from
      it - the element to transfer fromother to*this
      first, last - the pair of iterators defining therange of elements to transfer fromother to*this

      [edit]Complexity

      1-4) Constant.
      5,6) Constant ifother refers to the same object as*this, otherwise linear instd::distance(first, last).

      [edit]Example

      Run this code
      #include <iostream>#include <list> std::ostream& operator<<(std::ostream& ostr,conststd::list<int>& list){for(auto& i: list)        ostr<<' '<< i; return ostr;} int main(){std::list<int> list1{1,2,3,4,5};std::list<int> list2{10,20,30,40,50}; auto it= list1.begin();std::advance(it,2);     list1.splice(it, list2); std::cout<<"list1:"<< list1<<'\n';std::cout<<"list2:"<< list2<<'\n';     list2.splice(list2.begin(), list1, it, list1.end()); std::cout<<"list1:"<< list1<<'\n';std::cout<<"list2:"<< list2<<'\n';}

      Output:

      list1: 1 2 10 20 30 40 50 3 4 5list2:list1: 1 2 10 20 30 40 50list2: 3 4 5

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 250C++98references and iterators to the moved
      element(s) were all invalidated
      they refer or point to the
      same element(s) in*this
      N2525C++98O(1) splicing could not be guaranteed if
      get_allocator()!= other.get_allocator()
      the behavior is
      undefined in this case

      [edit]See also

      merges two sorted lists
      (public member function)[edit]
      removes elements satisfying specific criteria
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/list/splice&oldid=183367"

      [8]ページ先頭

      ©2009-2025 Movatter.jp