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

std::set_union

Defined in header <algorithm>

?

?

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

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > ForwardIt3 set_union( 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 set_union( 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 set_union( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, ForwardIt3 d_first, Compare comp );

(4)

(since C++17)

构造排序范围,从d_first由一个或两个排序范围中的所有元素组成[first1, last1)[first2, last2)...

如果找到某个元素m“时代”[first1, last1)n“时代”[first2, last2),然后所有m元素将从[first1, last1)d_first,维护秩序,然后std::max(n-m, 0)元素将从[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>>是真的

参数

first1, last1

-

the first input sorted range

first2, last2

-

the second input sorted 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必须符合先行者的要求。

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

返回值

在建造的范围结束后。

复杂性

最多2.%28N1+N2-1%29比较,其中N1=std::distance(first1, last1)和N2=std::distance(first2, last2)...

例外

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

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

注记

此算法执行的任务类似于std::merge是的。两者都使用两个已排序的输入范围,并从两个输入中生成带有元素的排序输出。这两种算法的不同之处在于处理来自两个输入范围的值,这些值比较等效的%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设置[医]UNION%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++;}Other{%2A丁[医]第一=%2A先1;如果%28%21%28%2A头1<%2A头2%29%29++first 2;++first 1;}返回std::拷贝%28first 2,last2,d[医]第一%29;}

第二版

模板<类InputIt 1,类InputIt 2,类OutputIt,类比较>OutputIt集[医]UNION%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++;}Other{%2A丁[医]第一=%2A首先1;if%28%21 comp%28%2A首先,%2A头2%29%29++first 2;++first 1;}返回std::拷贝%28first 2,last2,d[医]第一%29;}

向量示例:

二次

代码语言:javascript
复制
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
 
int main()
{
    std::vector<int> v1 = {1, 2, 3, 4, 5}; 
    std::vector<int> v2 = {      3, 4, 5, 6, 7}; 
    std::vector<int> dest1;
 
    std::set_union(v1.begin(), v1.end(),
                   v2.begin(), v2.end(),                  
                   std::back_inserter(dest1));
 
    for (const auto &i : dest1) {
        std::cout << i << ' ';
    }   
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
1 2 3 4 5 6 7

二次

另见

includes

returns true if one set is a subset of another (function template)

merge

merges two sorted ranges (function template)

set_difference

computes the difference between two sets (function template)

set_intersection

computes the intersection of two sets (function template)

set_symmetric_difference

computes the symmetric difference between two sets (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com