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

std::set_difference

Defined in header <algorithm>

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

(1)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 > ForwardIt3 set_difference( 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_difference( 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_difference( 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...

结果范围也被排序。如果找到某个元素,则单独处理等效元素。m“时代”[first1, last1)n“时代”[first2, last2),它将被复制到d_first一点儿没错std::max(m-n, 0)时代。结果范围不能与任何一个输入范围重叠。

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

first2, last2

-

the range of elements to search for

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设置[医]差%28 InputIt1 First 1,InputIt1 last1,InputIt2first 2,InputIt2last2,OutputIt d[医]第一%29{而%28first 1%21=最后1%29{if%28first 2=last2%29返回std::拷贝%28first 1,last1,d[医]%2A头1<%2A前2%29{%2A丁[医]第一++=%2A{if%28%21%28%2A头2<%2A头1%29%29{+头1;}+首2;}返回d[医]第一;}

第二版

模板<类InputIt 1,类InputIt 2,类OutputIt,类比较>OutputIt集[医]差异%28 InputIt1first 1,InputIt1last1,InputIt2first 2,InputIt2last2,OutputIt d[医]首先,比较comp%29{而%28first 1%21=last1%29{if%28first 2==last2%29返回std::Copy%28first 1,last1,d[医]第一%29;如果%28 Comp%28%2A首先,%2A前2%29%29{%2A丁[医]第一++=%2A先1++;}or{if%28%21 comp%28%2A第二,%2A头1%29%29{+头1;}+首2;}返回d[医]第一;}

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
 
int main() {
    std::vector<int> v1 {1, 2, 5, 5, 5, 9};
    std::vector<int> v2 {2, 5, 7};
    std::vector<int> diff;
 
    std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), 
                        std::inserter(diff, diff.begin()));
 
    for (auto i : v1) std::cout << i << ' ';
    std::cout << "minus ";
    for (auto i : v2) std::cout << i << ' ';
    std::cout << "is: ";
 
    for (auto i : diff) std::cout << i << ' ';
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
1 2 5 5 5 9 minus 2 5 7 is: 1 5 5 9

二次

另见

includes

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

set_symmetric_difference

computes the symmetric difference between two sets (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券