跳到主要内容

std::merge() 算法

// (1)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3 >
ForwardIt3 merge( 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 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

将两个已排序的范围 [first1; last1) 和 [first2; last2) 合并到一个以 d_first 开头的已排序范围中。

如果对于指向序列的任何迭代器 it 和任何非负整数 n(使得 it + n 是指向序列元素的有效迭代器),comp(*(it + n), *it) 评估为 false,则称序列相对于比较器 comp 是已排序的。

  • (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 起)

此合并函数是稳定的,这意味着对于原始两个范围中的等效元素,第一个范围中的元素优先于第二个范围中的元素,从而保留了它们的原始顺序。

未定义行为

行为未定义

如果目标范围与任一输入范围重叠(输入范围可以相互重叠)。

参数

first1
last2

要合并的第一个元素范围。

first2
last3

要合并的第二个元素范围。

d_first

目标范围的开头。

policy

要使用的执行策略。详见执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受所有(可能是 const)TypeType2 类型的值,无论值类别如何(因此不允许 Type1&除非对于 Type1 移动等效于复制,否则不允许 Type1  (自 C++11 起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
ForwardIt3
LegacyForwardIterator

返回值

指向复制的最后一个元素之后的元素的输出迭代器。

复杂度

给定 Nstd::distance(first1, last1) + std::distance(first2, last2)

  • (1) 最多使用 operator< 进行 N - 1 次比较。
  • (2) 使用 operator< 进行 O(N) 次比较。
  • (3) 最多使用 comp 进行 N - 1 次比较。
  • (4) 使用 comp 进行 O(N) 次比较。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

merge(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(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;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}
merge(2)
template<class InputIt1, class InputIt2,
class OutputIt, class Compare>
OutputIt merge(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;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}

备注

此算法执行与 std::set_union 类似的任务。两者都接受两个已排序的输入范围,并生成一个包含两个输入元素已排序的输出。

这两种算法之间的区别在于处理来自两个输入范围的等效值(请参阅LessThanComparable的注释)。

如果任何等效值在第一个范围中出现 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 可能会生成更少的值。

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

auto print = [](auto const rem, auto const& v)
{
std::cout << rem;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
};

int main()
{
// fill the vectors with random numbers
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0, 9);

std::vector<int> v1(10), v2(10);
std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));

print("Originally:\nv1: ", v1);
print("v2: ", v2);

std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

print("After sorting:\nv1: ", v1);
print("v2: ", v2);

// merge
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));

print("After merging:\ndst: ", dst);
}
可能的输出
Originally:
v1: 2 6 5 7 4 2 2 6 7 0
v2: 8 3 2 5 0 1 9 6 5 0
After sorting:
v1: 0 2 2 2 4 5 6 6 7 7
v2: 0 0 1 2 3 5 5 6 8 9
After merging:
dst: 0 0 0 1 2 2 2 2 3 4 5 5 5 6 6 6 7 7 8 9
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::merge() 算法

// (1)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3 >
ForwardIt3 merge( 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 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

将两个已排序的范围 [first1; last1) 和 [first2; last2) 合并到一个以 d_first 开头的已排序范围中。

如果对于指向序列的任何迭代器 it 和任何非负整数 n(使得 it + n 是指向序列元素的有效迭代器),comp(*(it + n), *it) 评估为 false,则称序列相对于比较器 comp 是已排序的。

  • (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 起)

此合并函数是稳定的,这意味着对于原始两个范围中的等效元素,第一个范围中的元素优先于第二个范围中的元素,从而保留了它们的原始顺序。

未定义行为

行为未定义

如果目标范围与任一输入范围重叠(输入范围可以相互重叠)。

参数

first1
last2

要合并的第一个元素范围。

first2
last3

要合并的第二个元素范围。

d_first

目标范围的开头。

policy

要使用的执行策略。详见执行策略

comp

比较函数对象(即满足 Compare 要求的对象)。比较函数的签名应等同于以下内容:

bool cmp(const Type1 &a, const Type2 &b);
  • 签名不需要有 `const&`,但不得修改参数。
  • 必须接受所有(可能是 const)TypeType2 类型的值,无论值类别如何(因此不允许 Type1&除非对于 Type1 移动等效于复制,否则不允许 Type1  (自 C++11 起)
  • `Type1` 和 `Type2` 类型必须是 `RandomIt` 类型的对象可以隐式转换为它们两者的类型。

类型要求

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
ForwardIt3
LegacyForwardIterator

返回值

指向复制的最后一个元素之后的元素的输出迭代器。

复杂度

给定 Nstd::distance(first1, last1) + std::distance(first2, last2)

  • (1) 最多使用 operator< 进行 N - 1 次比较。
  • (2) 使用 operator< 进行 O(N) 次比较。
  • (3) 最多使用 comp 进行 N - 1 次比较。
  • (4) 使用 comp 进行 O(N) 次比较。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

可能的实现

merge(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(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;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}
merge(2)
template<class InputIt1, class InputIt2,
class OutputIt, class Compare>
OutputIt merge(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;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}

备注

此算法执行与 std::set_union 类似的任务。两者都接受两个已排序的输入范围,并生成一个包含两个输入元素已排序的输出。

这两种算法之间的区别在于处理来自两个输入范围的等效值(请参阅LessThanComparable的注释)。

如果任何等效值在第一个范围中出现 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 可能会生成更少的值。

示例

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

auto print = [](auto const rem, auto const& v)
{
std::cout << rem;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
};

int main()
{
// fill the vectors with random numbers
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0, 9);

std::vector<int> v1(10), v2(10);
std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));

print("Originally:\nv1: ", v1);
print("v2: ", v2);

std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

print("After sorting:\nv1: ", v1);
print("v2: ", v2);

// merge
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));

print("After merging:\ndst: ", dst);
}
可能的输出
Originally:
v1: 2 6 5 7 4 2 2 6 7 0
v2: 8 3 2 5 0 1 9 6 5 0
After sorting:
v1: 0 2 2 2 4 5 6 6 7 7
v2: 0 0 1 2 3 5 5 6 8 9
After merging:
dst: 0 0 0 1 2 2 2 2 3 4 5 5 5 6 6 6 7 7 8 9
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。