首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::merge

Defined in header <algorithm>

?

?

template< class InputIt1, class InputIt2, class OutputIt > OutputIt merge( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > ForwardIt3 merge( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, ForwardIt3 d_first );

(2)

(since C++17)

template< class InputIt1, class InputIt2, class OutputIt, class Compare> OutputIt merge( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first, Compare comp );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class Compare> ForwardIt3 merge( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, ForwardIt3 d_first, Compare comp );

(4)

(since C++17)

合并两个排序范围[first1, last1)[first2, last2)进入一个排序范围,从d_first...

1%29个元素的比较operator<...

使用给定的二进制比较函数对3%29个元素进行比较。comp...

2,4%29与%281,3%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

对于原始两个范围中的等效元素,第一个范围%28中的元素保留其原始顺序%29,而来自第二个范围的元素%28保留其原始顺序%29。

如果目标范围与输入范围%28中的任何一个重叠,则该行为是未定义的,则输入范围可能彼此重叠%29。

参数

first1, last1

-

the first range of elements to merge

first2, last2

-

the second range of elements to merge

d_first

-

the beginning of the destination range

policy

-

the execution policy to use. See execution policy for details.

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to both Type1 and Type2. ?

类型要求

-InputIt 1,InputIt 2必须符合InputIterator的要求。

-前进It 1、前进It 2、前进It 3必须符合先行者的要求。

-输出必须符合输出器的要求。

返回值

一个输出迭代器,用于在上次复制的元素之后执行元素。

复杂性

最多1.3%29std::distance(first1, last1)+std::distance(first2, last2)-1比较。

2,4%29O(std::distance(last1, first1)+std::distance(last2, first2))

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

此算法执行的任务类似于std::set_union是的。两者都使用两个已排序的输入范围,并从两个输入中生成带有元素的排序输出。这两种算法的不同之处在于处理来自两个输入范围的值,这些值比较等效的%28--参见LessThanComparable29%。如果出现任何等效值n在第一个范围内m第二次,std::merge会输出所有n+m发生情况std::set_union会输出std::max(n, m)只有一个。所以std::merge精确输出std::distance(first1, last1)+std::distance(first2, last2)价值观和std::set_union可能产生的更少。

可能的实施

第一版

*。

模板<类InputIt 1,类InputIt 2,类OutputIt>OutputIt合并%28InputIt1first 1,InputIt1last1,InputIt2first 2,InputIt2last2,Outputit d[医]第1%29{表示%28;前1%21=LST 1;++d[医]第一%29{if%28first 2==last2%29{返回std::复制%28first 1,last1,d[医]第一%29;}如果%28%2A头2<%2A头1%29{%2A丁[医]第一=%2A前2;++第2;}or{%2A丁[医]第一=%2A返回std::复制%28first 2,LASST 2,d[医]第一%29;}

第二版

模板<类InputIt 1,类InputIt 2,类Outputit,类比较>OutputIt合并%28InputIt1First 1,InputIt1last1,InputIt2first 2,InputIt2last2,Outputit d[医]首先,比较comp%29{表示%28;前1%21=last1;++d[医]第一%29{if%28first 2==last2%29{返回std::复制%28first 1,last1,d[医]第一%29;}如果%28 Comp%28%2A第二,%2A头1%29%29{%2A丁[医]第一=%2A前2;++第2;}or{%2A丁[医]第一=%2A返回std::复制%28first 2,LASST 2,d[医]第一%29;}

二次

代码语言:javascript
复制
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <random>
#include <functional>
 
int main()
{
    // fill the vectors with random numbers
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_int_distribution<> dis(0, 9);
 
    std::vector<int> v1(10), v2(10);
    std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
    std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));
 
    // sort
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());
 
    // output v1
    std::cout << "v1 : ";
    std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    // output v2
    std::cout << "v2 : ";
    std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    // merge
    std::vector<int> dst;
    std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
 
    // output
    std::cout << "dst: ";
    std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
v1 : 0 1 3 4 4 5 5 8 8 9 
v2 : 0 2 2 3 6 6 8 8 8 9 
dst: 0 0 1 2 2 3 3 4 4 5 5 6 6 8 8 8 8 8 9 9

二次

另见

inplace_merge

merges two ordered ranges in-place (function template)

set_union

computes the union of two sets (function template)

sort

sorts a range into ascending order (function template)

stable_sort

sorts a range of elements while preserving order between equal elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com