std::set_intersection() 算法
- 自 C++20 起
- 自 C++17 起
- C++17 之前
// (1)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt set_intersection( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first );
// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr 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 >
ForwardIt3 set_intersection( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first );
// (4)
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 );
// (1)
template< class InputIt1, class InputIt2, class OutputIt >
OutputIt set_intersection( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first );
// (2)
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 >
ForwardIt3 set_intersection( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first );
// (4)
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 );
// (1)
template< class InputIt1, class InputIt2, class OutputIt >
OutputIt set_intersection( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first );
// (2)
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 );
构造一个始于 d_first
的已排序范围,其中包含在两个已排序范围 [first1
; last1
) 和 [first2
; last2
) 中都找到的元素。
如果 [first1
; last1
) 包含 m
个相互等效的元素,并且 [first2
; last2
) 包含 n
个与它们等效的元素,则最终将 std::max(m, n)
个元素从 [first1
; last1
) 复制到输出范围,并保留顺序。
-
(1) 两个范围都必须根据
operator<
进行排序。 -
(2) 两个范围都必须根据
comp
进行排序。 -
(3 - 4) 同 (1) 和 (2),但根据策略执行。
重载决议这些重载只有在
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)
如果任何输入范围未排序(分别使用 operator<
或 comp
),或者与输出范围重叠,则行为未定义
参数
first1 last2 | 要检查的第一个已排序元素范围。 |
first2 last3 | 要检查的第二个已排序元素范围。 |
d_first | 目标范围的开头。 |
policy | 要使用的执行策略。详见执行策略。 |
comp | 比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:
|
类型要求
InputIt1 InputIt2 | LegacyInputIterator |
ForwardIt1 ForwardIt2 ForwardIt3 | LegacyForwardIterator |
OutputIt1 | LegacyOutputIterator |
返回值
构造范围末尾之后的迭代器。
复杂度
给定 M
为 std::distance(first1, last1),N
为 std::distance(first2, last2)
- (1, 3) 最多使用
operator<
进行 2 * (M + N) - 1 次比较。 - (2, 4) 最多使用
comp
进行 2 * (M + N) - 1 次比较。
异常
带有模板参数 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 and *first2 are equivalent.
++first2;
}
}
return d_first;
}
set_intersection(2)
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 and *first2 are equivalent.
++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';
}
5 7 7