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

std::set_intersection

Defined in header <algorithm>

?

?

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

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > ForwardIt3 set_intersection( 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_intersection( 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_intersection( 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),第一个std::min(m, n)元素将从第一个范围复制到目标范围。保持等效元素的顺序。结果范围不能与任何一个输入范围重叠。

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 range of elements to examine

first2, last2

-

the second range of elements to examine

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被扔了。

可能的实施

第一版

*。

模板<类InputIt 1,类InputIt 2,类OutputIt>OutputIt设置[医]交集%28InputIt1first 1,InputIt1last1,InputIt2first 2,InputIt2last2,OutputIt d[医]第一%29{而%28first 1%21=LST 1&&前2%21=LASST 2%29{if%28%2A头1<%2A前2%29{++开头1;}{if%28%21%28%2A头2<%2A头1%29%29{%2A丁[医]第一++=%2A前1++;}++开头2;}返回d[医]第一;}

第二版

模板<类InputIt 1,类InputIt 2,类OutputIt,类比较>OutputIt集[医]交集%28InputIt1first 1,InputIt1last1,InputIt2first 2,InputIt2last2,OutputIt d[医]首先,比较comp%29{,而%28first 1%21=last1&&first 2%21=last2%29{if%28 comp%28。%2A首先,%2A前2%29%29{++first 1;}{if%28%21 comp%28%2A第二,%2A头1%29%29{%2A丁[医]第一++=%2A前1++;}++开头2;}返回d[医]第一;}

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
    std::vector<int> v1{1,2,3,4,5,6,7,8};
    std::vector<int> v2{        5,  7,  9,10};
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());
 
    std::vector<int> v_intersection;
 
    std::set_intersection(v1.begin(), v1.end(),
                          v2.begin(), v2.end(),
                          std::back_inserter(v_intersection));
    for(int n : v_intersection)
        std::cout << n << ' ';
}

二次

产出:

二次

代码语言:javascript
复制
5 7

二次

另见

set_union

computes the union of two sets (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com