跳到主要内容

std::set_difference() 算法

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

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr 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 >
ForwardIt3 set_difference( 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_difference( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

将已排序范围 [first1; last1) 中未在已排序范围 [first2; last2) 中找到的元素复制到从 d_first 开始的范围。输出范围也是已排序的。

如果 [first1; last1) 包含 m 个彼此等效的元素,并且 [first2; last2) 包含 n 个与它们等效的元素,则将从 [first1; last1) 中复制最终的 std::max(m - n, 0) 个元素到输出范围,并保留顺序。

  • (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 要求的对象)。比较函数的签名应等同于以下内容:

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
OutputIt1LegacyOutputIterator

返回值

构造范围末尾之后的迭代器。

复杂度

给定 M 为 std::distance(first1, last1),Nstd::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_difference(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_difference(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first)
{
while (first1 != last1)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (*first1 < *first2)
*d_first++ = *first1++;
else
{
if (! (*first2 < *first1))
++first1;
++first2;
}
}
return d_first;
}
set_difference(2)
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)
{
while (first1 != last1)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (comp(*first1, *first2))
*d_first++ = *first1++;
else
{
if (!comp(*first2, *first1))
++first1;
++first2;
}
}
return d_first;
}

示例

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

template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
for (os << "{ "; auto const& e : v)
os << e << ' ';
return os << '}';
}

struct Order // a struct with some interesting data
{
int order_id{};

friend std::ostream& operator<<(std::ostream& os, const Order& ord)
{
return os << ord.order_id;
}
};

int main()
{
const std::vector<int> v1 {1, 2, 5, 5, 5, 9};
const 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()));

std::cout << v1 << " ∖ " << v2 << " = " << diff << '\n';

// we want to know which orders "cut" between old and new states:
std::vector<Order> old_orders {{1}, {2}, {5}, {9}};
std::vector<Order> new_orders {{2}, {5}, {7}};
std::vector<Order> cut_orders;

std::set_difference(old_orders.begin(), old_orders.end(),
new_orders.begin(), new_orders.end(),
std::back_inserter(cut_orders),
[](auto& a, auto& b) { return a.order_id < b.order_id; });

std::cout << "old orders = " << old_orders << '\n'
<< "new orders = " << new_orders << '\n'
<< "cut orders = " << cut_orders << '\n';
}
可能的输出
{ 1 2 5 5 5 9 } ∖ { 2 5 7 } = { 1 5 5 9 } 
old orders = { 1 2 5 9 }
new orders = { 2 5 7 }
cut orders = { 1 9 }
本文源自 此 CppReference 页面。它可能经过修改以进行改进或满足编辑者的偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::set_difference() 算法

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

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr 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 >
ForwardIt3 set_difference( 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_difference( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

将已排序范围 [first1; last1) 中未在已排序范围 [first2; last2) 中找到的元素复制到从 d_first 开始的范围。输出范围也是已排序的。

如果 [first1; last1) 包含 m 个彼此等效的元素,并且 [first2; last2) 包含 n 个与它们等效的元素,则将从 [first1; last1) 中复制最终的 std::max(m - n, 0) 个元素到输出范围,并保留顺序。

  • (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 要求的对象)。比较函数的签名应等同于以下内容:

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
OutputIt1LegacyOutputIterator

返回值

构造范围末尾之后的迭代器。

复杂度

给定 M 为 std::distance(first1, last1),Nstd::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_difference(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_difference(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, OutputIt d_first)
{
while (first1 != last1)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (*first1 < *first2)
*d_first++ = *first1++;
else
{
if (! (*first2 < *first1))
++first1;
++first2;
}
}
return d_first;
}
set_difference(2)
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)
{
while (first1 != last1)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (comp(*first1, *first2))
*d_first++ = *first1++;
else
{
if (!comp(*first2, *first1))
++first1;
++first2;
}
}
return d_first;
}

示例

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

template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
for (os << "{ "; auto const& e : v)
os << e << ' ';
return os << '}';
}

struct Order // a struct with some interesting data
{
int order_id{};

friend std::ostream& operator<<(std::ostream& os, const Order& ord)
{
return os << ord.order_id;
}
};

int main()
{
const std::vector<int> v1 {1, 2, 5, 5, 5, 9};
const 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()));

std::cout << v1 << " ∖ " << v2 << " = " << diff << '\n';

// we want to know which orders "cut" between old and new states:
std::vector<Order> old_orders {{1}, {2}, {5}, {9}};
std::vector<Order> new_orders {{2}, {5}, {7}};
std::vector<Order> cut_orders;

std::set_difference(old_orders.begin(), old_orders.end(),
new_orders.begin(), new_orders.end(),
std::back_inserter(cut_orders),
[](auto& a, auto& b) { return a.order_id < b.order_id; });

std::cout << "old orders = " << old_orders << '\n'
<< "new orders = " << new_orders << '\n'
<< "cut orders = " << cut_orders << '\n';
}
可能的输出
{ 1 2 5 5 5 9 } ∖ { 2 5 7 } = { 1 5 5 9 } 
old orders = { 1 2 5 9 }
new orders = { 2 5 7 }
cut orders = { 1 9 }
本文源自 此 CppReference 页面。它可能经过修改以进行改进或满足编辑者的偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。