std::set_union
| 在标头 <algorithm> 定义
|
||
template< class InputIt1, class InputIt2, class OutputIt > OutputIt set_union( 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_union( 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_union( 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_union( 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) 保持顺序地复制全部 m 个元素到输出范围,然后从 [first2, last2) 保持顺序地复制最后 std::max(n - m, 0) 个元素到输出范围。
[first1, last1) 或 [first2, last2) 没有按 comp 排序,那么行为未定义。policy 执行。|
|
(C++20 前) |
|
|
(C++20 起) |
如果输出范围与 [first1, last1) 或 [first2, last2) 重叠,那么行为未定义。
参数
| first1, last1 | - | 第一个输入的有序元素范围的迭代器对 |
| first2, last2 | - | 第二个输入的有序元素范围的迭代器对 |
| d_first | - | 输出范围的起始 |
| policy | - | 所用的执行策略 |
| comp | - | 比较函数对象(即满足比较 (Compare) 概念的对象),在第一参数小于(即先 序于)第二参数时返回 true。比较函数的签名应等价于如下:
虽然签名不必有 |
| 类型要求 | ||
-InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt1, ForwardIt2, ForwardIt3 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
| ||
-Compare 必须满足比较 (Compare) 。
| ||
返回值
所构造范围的尾后迭代器。
复杂度
给定 N1 为 std::distance(first1, last1),N2 为 std::distance(first2, last2):
operator<(C++20 前)std::less{}(C++20 起) 进行比较。comp。异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| set_union (1) |
|---|
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);
if (*first2 < *first1)
*d_first = *first2++;
else
{
*d_first = *first1;
if (!(*first1 < *first2))
++first2;
++first1;
}
}
return std::copy(first2, last2, d_first);
}
|
| set_union (3) |
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)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
// 结束第二个范围,包含第一个范围的剩余:
return std::copy(first1, last1, d_first);
if (comp(*first2, *first1))
*d_first = *first2++;
else
{
*d_first = *first1;
if (!comp(*first1, *first2)) // 等价 => 不需要包含 *first2.
++first2;
++first1;
}
}
// 结束第二个范围,包含第二个范围的剩余:
return std::copy(first2, last2, d_first);
}
|
注解
此算法进行与 std::merge 相似的任务。两个算法都会消耗两个输入范围,并以来自两个输入的元素产生一个有序输出。这两个算法间的区别在于如何处理来自两个输入范围的比较为等价的值(见可比较小于的注解)。如果任何等价的值在第一范围中出现 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 可能产生较少的值。
示例
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void println(const std::vector<int>& v)
{
for (int i : v)
std::cout << i << ' ';
std::cout << '\n';
}
int main()
{
std::vector<int> v1, v2, dest;
v1 = {1, 2, 3, 4, 5};
v2 = {3, 4, 5, 6, 7};
std::set_union(v1.cbegin(), v1.cend(),
v2.cbegin(), v2.cend(),
std::back_inserter(dest));
println(dest);
dest.clear();
v1 = {1, 2, 3, 4, 5, 5, 5};
v2 = {3, 4, 5, 6, 7};
std::set_union(v1.cbegin(), v1.cend(),
v2.cbegin(), v2.cend(),
std::back_inserter(dest));
println(dest);
}
输出:
1 2 3 4 5 6 7
1 2 3 4 5 5 5 6 7
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 291 | C++98 | 未指定如何处理输入范围中等价的值 | 已指定处理方法 |
参阅
当一个序列是另一个的子序列时返回 true (函数模板) | |
| 合并两个有序范围 (函数模板) | |
| 计算两个集合的差集 (函数模板) | |
| 计算两个集合的交集 (函数模板) | |
| 计算两个集合的对称差 (函数模板) | |
(C++20) |
计算两个集合的并集 (算法函数对象) |