template< class InputIt1, class InputIt2, class OutputIt >
OutputIt set_intersection( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );
|
(1) |
(C++20 起为 constexpr) |
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) |
(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) |
(C++20 起为 constexpr) |
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) |
(C++17 起) |
| | |
构造始于 d_first,由在两个有序范围 [first1, last1) 与 [first2, last2) 中都找到的元素构成的有序范围。
如果 [first1, last1) 中有 m 个互相等价的元素,并且 [first2, last2) 中有 n 个与它们等价的元素,那么将从 [first1, last1) 保持顺序地复制前 std::min(m, n) 个元素到输出范围。
1) 如果
[first1, last1) 或
[first2, last2) 没有按
operator<(C++20 前)std::less{}(C++20 起) 排序,那么行为未定义。
3) 如果 [first1, last1) 或 [first2, last2) 没有按 comp 排序,那么行为未定义。
2,4) 同 (1,3),但按照 policy 执行。
这些重载只有在满足以下所有条件时才会参与重载决议:
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 是 true。
|
(C++20 前) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 是 true。
|
(C++20 起) |
如果输出范围与 [first1, last1) 或 [first2, last2) 重叠,那么行为未定义。
参数
| first1, last1
|
-
|
要检验的第一个有序元素范围的迭代器对
|
| first2, last2
|
-
|
要检验的第二个有序元素范围的迭代器对
|
| d_first
|
-
|
输出范围的起始
|
| policy
|
-
|
所用的执行策略
|
| comp
|
-
|
比较函数对象(即满足比较 (Compare) 概念的对象),在第一参数小于(即先 序于)第二参数时返回 true。
比较函数的签名应等价于如下:
bool cmp(const Type1 &a, const Type2 &b);
虽然签名不必有 const&,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1 与 Type2 的值,无关乎值类别(从而不允许 Type1& ,也不允许 Type1,除非 Type1 的移动等价于复制(C++11 起))。
类型 Type1 与 Type2 必须使得 InputIt1 与 InputIt2 类型的对象在解引用后能隐式转换到 Type1 与 Type2 两者。
|
| 类型要求
|
-InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
|
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
|
-ForwardIt1, ForwardIt2, ForwardIt3 必须满足老式向前迭代器 (LegacyForwardIterator) 。
|
-Compare 必须满足比较 (Compare) 。
|
返回值
构造范围的尾后迭代器。
复杂度
给定 \(\scriptsize N_1\)N1 为 std::distance(first1, last1),\(\scriptsize N_2\)N2 为 std::distance(first2, last2):
1,2) 最多应用 \(\scriptsize 2 \cdot (N_1+N_2)-1\)2⋅(N1+N2)-1 次 operator<(C++20 前)std::less{}(C++20 起) 进行比较。
3,4) 最多应用 \(\scriptsize 2 \cdot (N_1+N_2)-1\)2⋅(N1+N2)-1 次比较函数 comp。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy 是标准策略之一,那么调用 std::terminate。对于任何其他 ExecutionPolicy,行为由实现定义。
- 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| set_intersection (1)
|
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first)
{
while (first1 != last1 && first2 != last2)
{
if (*first1 < *first2)
++first1;
else
{
if (!(*first2 < *first1))
*d_first++ = *first1++; // *first1 和 *first2 等价。
++first2;
}
}
return d_first;
}
|
| set_intersection (3)
|
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)
{
while (first1 != last1 && first2 != last2)
{
if (comp(*first1, *first2))
++first1;
else
{
if (!comp(*first2, *first1))
*d_first++ = *first1++; // *first1 和 *first2 等价。
++first2;
}
}
return d_first;
}
|
示例
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v1{7, 2, 3, 4, 5, 6, 7, 8};
std::vector<int> v2{5, 7, 9, 7};
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 << ' ';
std::cout << '\n';
}
输出:
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告
|
应用于
|
出版时的行为
|
正确行为
|
| LWG 291
|
C++98
|
未指定如何处理输入范围中等价的值
|
已指定处理方法
|
参阅
|
|
计算两个集合的并集 (函数模板) |
|
|
计算两个集合的交集 (算法函数对象) |