Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inplace_merge

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      inplace_merge
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Defined in header<algorithm>
      template<class BidirIt>
      void inplace_merge( BidirIt first, BidirIt middle, BidirIt last);
      (1)(constexpr since C++26)
      template<class ExecutionPolicy,class BidirIt>

      void inplace_merge( ExecutionPolicy&& policy,

                          BidirIt first, BidirIt middle, BidirIt last);
      (2)(since C++17)
      template<class BidirIt,class Compare>

      void inplace_merge( BidirIt first, BidirIt middle, BidirIt last,

                          Compare comp);
      (3)(constexpr since C++26)
      template<class ExecutionPolicy,class BidirIt,class Compare>

      void inplace_merge( ExecutionPolicy&& policy,
                          BidirIt first, BidirIt middle, BidirIt last,

                          Compare comp);
      (4)(since C++17)

      Merges two consecutive sorted ranges[firstmiddle) and[middlelast) into one sorted range[firstlast).

      1) If[firstmiddle) or[middlelast) is notsorted with respect tooperator<(until C++20)std::less{}(since C++20), the behavior is undefined.
      3) If[firstmiddle) or[middlelast) is not sorted with respect tocomp, the behavior is undefined.
      2,4) Same as(1,3), but executed according topolicy.
      These overloads participate in overload resolution only if all following conditions are satisfied:

      std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue.

      (until C++20)

      std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue.

      (since C++20)

      This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).

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

      • [firstmiddle) or[middlelast) is not avalid range.
      (until C++11)
      (since C++11)

      Contents

      [edit]Parameters

      first - the beginning of the first sorted range
      middle - the end of the first sorted range and the beginning of the second
      last - the end of the second sorted range
      policy - theexecution policy to use
      comp - comparison function object (i.e. an object that satisfies the requirements ofCompare) which returns ​true if the first argument isless than (i.e. is orderedbefore) the second.

      The signature of the comparison function should be equivalent to the following:

      bool cmp(const Type1& a,const Type2& b);

      While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const)Type1 andType2 regardless ofvalue category (thus,Type1& is not allowed, nor isType1 unless forType1 a move is equivalent to a copy(since C++11)).
      The typesType1 andType2 must be such that an object of typeBidirIt can be dereferenced and then implicitly converted to both of them.​

      Type requirements
      -
      BidirIt must meet the requirements ofLegacyBidirectionalIterator.
      -
      Compare must meet the requirements ofCompare.

      [edit]Complexity

      Given\(\scriptsize N\)N asstd::distance(first, last):

      1) Exactly\(\scriptsize N-1\)N-1 comparisons usingoperator<(until C++20)std::less{}(since C++20) if enough additional memory is available,\(\scriptsize O(N \cdot \log(N))\)O(N⋅log(N)) comparisons otherwise.
      2)\(\scriptsize O(N \cdot \log(N))\)O(N⋅log(N)) comparisons usingoperator<(until C++20)std::less{}(since C++20).
      3) Exactly\(\scriptsize N-1\)N-1 applications of the comparison functioncomp if enough additional memory is available,\(\scriptsize O(N \cdot \log(N))\)O(N⋅log(N)) applications otherwise.
      4)\(\scriptsize O(N \cdot \log(N))\)O(N⋅log(N)) applications of the comparison functioncomp.

      [edit]Exceptions

      The overloads with a template parameter namedExecutionPolicy report errors as follows:

      • If execution of a function invoked as part of the algorithm throws an exception andExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.
      • If the algorithm fails to allocate memory,std::bad_alloc is thrown.

      [edit]Possible implementation

      See the implementations inlibstdc++ andlibc++.

      [edit]Notes

      This function attempts to allocate a temporary buffer. If the allocation fails, the less efficient algorithm is chosen.

      Feature-test macroValueStdFeature
      __cpp_lib_constexpr_algorithms202306L(C++26)constexpr inplace merging(1),(3)

      [edit]Example

      The following code is an implementation of merge sort.

      Run this code
      #include <algorithm>#include <iostream>#include <vector> template<class Iter>void merge_sort(Iter first, Iter last){if(last- first>1){        Iter middle= first+(last- first)/2;        merge_sort(first, middle);        merge_sort(middle, last);        std::inplace_merge(first, middle, last);}} int main(){std::vector<int> v{8,2,-2,0,11,11,1,7,3};    merge_sort(v.begin(), v.end());for(constauto& n: v)std::cout<< n<<' ';std::cout<<'\n';}

      Output:

      -2 0 1 2 3 7 8 11 11

      [edit]See also

      merges two sorted ranges
      (function template)[edit]
      sorts a range into ascending order
      (function template)[edit]
      sorts a range of elements while preserving order between equal elements
      (function template)[edit]
      merges two ordered ranges in-place
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/inplace_merge&oldid=179790"

      [8]ページ先頭

      ©2009-2025 Movatter.jp