算法库
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last)
,其中 last
指代要查询或修改的最后元素的后一个元素。
应用一个函数对象,然后以乱序规约(函数模板)
std::transform_reduce
template<class InputIt1, class InputIt2, class T> | (1) | (C++17 起) |
template <class InputIt1, class InputIt2, class T, class BinaryOp1, class BinaryOp2> T transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOp1 binary_op1, BinaryOp2 binary_op2); | (2) | (C++17 起) |
template<class InputIt, class T, class BinaryOp, class UnaryOp> T transform_reduce(InputIt first, InputIt last, T init, BinaryOp binop, UnaryOp unary_op); | (3) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T> | (4) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T, class BinaryOp1, class BinaryOp2> | (5) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp, class UnaryOp> | (6) | (C++17 起) |
1) 等价于 transform_reduce(first1, last1, first2, init, std::plus<>(), std::multiplies<>());
,默认的 std::inner_product 的等效并行版本
2) 应用 binary_op2
到来自范围 [first; last) 和始于 first2 的范围的每对元素,并在 binary_op1
上与初始值 init
一同规约结果(可以以未指定行为重排聚合)
3) 应用 unary_op
到范围 [first; last) 中的每个元素,并在 binary_op
上与初始值 init
一同规约结果(可以以未指定行为重排聚合)。
4-6) 同 (1-3) ,但按照 policy
执行。此重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true才参与重载决议
若 binary_op
/binary_op2
为非交换或非结合则行为不确定。
若 unary_op
、 binary_op
、 binary_op1
或 binary_op2
修改输入范围中的任何元素或非法化范围中的任何迭代器,含尾迭代器,则行为未定义。
参数
first, last | - | 要应用算法的元素范围 |
init | - | 广义和的初始值 |
policy | - | 使用的执行策略,细节见执行策略。 |
unary_op | - | 将应用于输入范围的每个元素的一元函数对象 (FunctionObject) 。返回类型必须可为 binary_op 的输入所接受 |
binary_op | - | 将以未指定顺序应用于 unary_op 的结果、其他 binary_op 的结果和 init 的二元函数对象 (FunctionObject) 。 |
类型要求 | ||
- 为使用重载 (3,6) , T 必须满足可移动构造 (MoveConstructible) 的要求。且表达式 binary_op(init, unary_op(*first)) 、 binary_op(unary_op(*first), init) 、 binary_op(init, init) 和 binary_op(unary_op(*first), unary_op(*first)) 的结果必须可转换为 T | ||
- 为使用重载 (2,5) , T 必须满足可移动构造 (MoveConstructible) 的要求。且表达式 binary_op1(init, binary_op2(*first1, *first2)) 、 binary_op1(binary_op2(*first1, *first2), init) 、 binary_op1(init, init) 和 binary_op1(binary_op2(*first1, *first2), binary_op2(*first1, *first2)) 的结果必须可转换为 T | ||
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 |
返回值
2) init
和 binary_op2(*first,*first2)
、 binary_op2(*(first+1),*(first2+1))
……在 binary_op1
上的广义和
3) init
和 unary_op(*first)
、 unary_op(*(first+1))
…… unary_op(*(last-1))
在 binary_op
上的广义和,
其中广义和 GSUM(op, a
1, ..., a
N) 定义如下:
- 若 N=1 ,则为 a
1 - 若 N > 1 ,则为 op(GSUM(op, b
1, ..., b
K), GSUM(op, b
M, ..., b
N)) ,其中
- b
1, ..., b
N 可以是 a1, ..., aN 的任意重排,若 - 1 < K+1 = M ≤ N
换言之, unary_op 或 binary_op1 的结果能以任意顺序组合排列。
复杂度
1,2,4,5) 各使用 O(last1 - first1) 次 binary_op1
和 binary_op2
。
3,6) 各使用 O(last - first) 次 unary_op
和 binary_op
。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 若作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc 。
注意
在一元-二元重载 (3,6) 中, unary_op
不应用于 init
若 first == last
或 first1 == last1
,则返回不修改的 init
调用示例
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <random>
#include <vector>
#include <cassert>
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
friend Cell operator+(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x += rcell.x;
cell.y += rcell.y;
return cell;
}
friend Cell operator-(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x -= rcell.x;
cell.y -= rcell.y;
return cell;
}
friend Cell operator*(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x *= rcell.x;
cell.y *= rcell.y;
return cell;
}
friend Cell operator/(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x /= rcell.x;
cell.y /= rcell.y;
return cell;
}
friend Cell operator%(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x %= rcell.x;
cell.y %= rcell.y;
return cell;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
namespace std
{
//1. `first` 和 `last`:表示输入序列的起始和结束迭代器。
//2. `unary_op`:一个一元操作符,用于对输入序列中的每个元素进行变换。
//3. `binary_op`:一个二元操作符,用于将变换后的元素累积到结果中。
template<class InputIt, class UnaryOperation, class BinaryOperation>
auto transform_reduce(InputIt first, InputIt last, UnaryOperation unary_op, BinaryOperation binary_op)->decltype(*first)
{
using ValueType = decltype(*first);
ValueType result = *first;
++first;
for (; first != last; ++first)
{
result = binary_op(result, unary_op(*first));
*first = result;
}
return result;
}
}
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
auto generate10 = []()
{
int n = std::rand() % 10 + 1;
Cell cell{n, n};
return cell;
};
auto Unaryfunction = [](const Cell & c_cell)
{
Cell cell = c_cell + Cell{1, 1};
return cell;
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector1(8, generate());
std::generate(vector1.begin(), vector1.end(), generate);
std::sort(vector1.begin(), vector1.end());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::vector<Cell> vector2(vector1.size(), Cell{101, 101});
std::generate(vector2.begin(), vector2.end(), generate);
std::sort(vector2.begin(), vector2.end());
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::vector<Cell> vector3(8, generate10());
std::generate(vector3.begin(), vector3.end(), generate10);
std::sort(vector3.begin(), vector3.end());
std::cout << "vector3: ";
std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::transform_reduce(vector1.begin(), vector1.end(), Unaryfunction, std::plus<Cell>());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
std::transform_reduce(vector1.begin(), vector1.end(), Unaryfunction, std::minus<Cell>());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
std::transform_reduce(vector1.begin(), vector1.end(), Unaryfunction, std::multiplies<Cell>());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
std::transform_reduce(vector1.begin(), vector1.end(), Unaryfunction, std::divides<Cell>());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
std::transform_reduce(vector1.begin(), vector1.end(), Unaryfunction, std::modulus<Cell>());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
return 0;
}
输出
vector1: {110,110} {113,113} {113,113} {114,114} {114,114} {116,116} {117,117} {119,119}
vector2: {110,110} {110,110} {110,110} {113,113} {113,113} {113,113} {115,115} {116,116}
vector3: {2,2} {3,3} {3,3} {4,4} {4,4} {7,7} {9,9} {9,9}
vector1: {923,923} {224,224} {338,338} {453,453} {568,568} {685,685} {803,803} {923,923}
vector1: {-3078,-3078} {698,698} {359,359} {-95,-95} {-664,-664} {-1350,-1350} {-2154,-2154} {-3078,-3078}
vector1: {-349004128,-349004128} {-2151522,-2151522} {-774547920,-774547920} {-206939552,-206939552} {-238030496,-238030496} {-1019408096,-1019408096} {57342432,57342432} {-349004128,-349004128}
vector1: {0,0} {162,162} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
vector1: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}