C++11标准模板(STL)- 算法库 - 类似 std::accumulate,但不依序执行 -(std::reduce)

devtools/2024/10/18 23:23:56/

算法

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

类似 std::accumulate,但不依序执行

std::reduce
template<class InputIt>

typename std::iterator_traits<InputIt>::value_type reduce(

    InputIt first, InputIt last);
(1)(C++17 起)
template<class ExecutionPolicy, class ForwardIt>

typename std::iterator_traits<ForwardIt>::value_type reduce(
    ExecutionPolicy&& policy,

    ForwardIt first, ForwardIt last);
(2)(C++17 起)

template<class InputIt, class T>
T reduce(InputIt first, InputIt last, T init);

(3)(C++17 起)
template<class ExecutionPolicy, class ForwardIt, class T>

T reduce(ExecutionPolicy&& policy,

         ForwardIt first, ForwardIt last, T init);
(4)(C++17 起)

template<class InputIt, class T, class BinaryOp>
T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op);

(5)(C++17 起)
template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp>

T reduce(ExecutionPolicy&& policy,

         ForwardIt first, ForwardIt last, T init, BinaryOp binary_op);
(6)(C++17 起)

1) 同 reduce(first, last, typename std::iterator_traits<InputIt>::value_type{})

3) 同 reduce(first, last, init, std::plus<>())

5) 在 binary_op 上以初值 init 规约范围 [first; last) ,可能以未指定方式排序聚合。

2,4,6) 同 (1,3,5) ,但按照 policy 执行。此重载仅若std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true才参与重载决议

binary_op 非结合或非交换,则行为非确定。

binary_op 修改 [first; last] 中任何元素或非法化范围中任何迭代器,含尾迭代器,则行为未定义。

参数

first, last-要应用算法的元素范围
init-广义和的初值
policy-使用的执行策略。细节见执行策略。
binary_op-将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject) 。
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- T 必须满足可移动构造 (MoveConstructible) 的要求。而且 binary_op(init, *first)binary_op(*first, init)binary_op(init, init)binary_op(*first, *first) 必须可转换到 T

返回值

init*first*(first+1) 、…… *(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

换言之, reduce 表现类似 std::accumulate ,除了范围中的元素可能以任意顺序分组并重排。

复杂度

O(last - first) 次应用 binary_op.

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

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

注意

若为空,则返回不修改的 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
{
template <typename InputIt, typename T, typename BinaryOperation>
T reduce(InputIt first, InputIt last, T init, BinaryOperation op)
{for (; first != last; ++first){init = op(std::move(init), *first);}return init;
}
}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;};//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(), generate());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;for (size_t index = 0; index < vector1.size(); ++index){std::cout << "std::reduce(vector1.begin(), " << index << ", Cell{0, 0} std::plus<Cell>() ):    ";std::cout << std::reduce(vector1.begin(), vector1.begin() + index, Cell{0, 0}, std::plus<Cell>());std::cout << std::endl;}std::cout << std::endl;for (size_t index = 0; index < vector1.size(); ++index){std::cout << "std::reduce(vector1.begin(), " << index << ", Cell{0, 0} std::minus<Cell>() ):    ";std::cout << std::reduce(vector1.begin(), vector1.begin() + index, Cell{0, 0}, std::minus<Cell>());std::cout << std::endl;}std::cout << std::endl;for (size_t index = 0; index < vector2.size(); ++index){std::cout << "std::reduce(vector2.begin(), " << index << ", Cell{1, 1} std::multiplies<Cell>() ):    ";std::cout << std::reduce(vector2.begin(), vector2.begin() + index, Cell{1, 1}, std::multiplies<Cell>());std::cout << std::endl;}std::cout << std::endl;for (size_t index = 0; index < vector2.size(); ++index){std::cout << "std::reduce(vector2.begin(), " << index << ", Cell{1024, 1024} std::divides<Cell>() ):    ";std::cout << std::reduce(vector2.begin(), vector2.begin() + index, Cell{1024, 1024}, std::divides<Cell>());std::cout << std::endl;}std::cout << std::endl;for (size_t index = 0; index < vector2.size(); ++index){std::cout << "std::reduce(vector2.begin(), " << index << ", Cell{1024, 1024} std::modulus<Cell>() ):    ";std::cout << std::reduce(vector2.begin(), vector2.begin() + index, Cell{1024, 1024}, std::modulus<Cell>());std::cout << std::endl;}std::cout << std::endl;return 0;
}

输出

vector1:  {111,111} {112,112} {113,113} {115,115} {116,116} {116,116} {117,117} {119,119}
vector2:  {110,110} {112,112} {112,112} {114,114} {117,117} {117,117} {119,119} {119,119}
std::reduce(vector1.begin(), 0, Cell{0, 0} std::plus<Cell>() ):    {0,0}
std::reduce(vector1.begin(), 1, Cell{0, 0} std::plus<Cell>() ):    {111,111}
std::reduce(vector1.begin(), 2, Cell{0, 0} std::plus<Cell>() ):    {223,223}
std::reduce(vector1.begin(), 3, Cell{0, 0} std::plus<Cell>() ):    {336,336}
std::reduce(vector1.begin(), 4, Cell{0, 0} std::plus<Cell>() ):    {451,451}
std::reduce(vector1.begin(), 5, Cell{0, 0} std::plus<Cell>() ):    {567,567}
std::reduce(vector1.begin(), 6, Cell{0, 0} std::plus<Cell>() ):    {683,683}
std::reduce(vector1.begin(), 7, Cell{0, 0} std::plus<Cell>() ):    {800,800}std::reduce(vector1.begin(), 0, Cell{0, 0} std::minus<Cell>() ):    {0,0}
std::reduce(vector1.begin(), 1, Cell{0, 0} std::minus<Cell>() ):    {-111,-111}
std::reduce(vector1.begin(), 2, Cell{0, 0} std::minus<Cell>() ):    {-223,-223}
std::reduce(vector1.begin(), 3, Cell{0, 0} std::minus<Cell>() ):    {-336,-336}
std::reduce(vector1.begin(), 4, Cell{0, 0} std::minus<Cell>() ):    {-451,-451}
std::reduce(vector1.begin(), 5, Cell{0, 0} std::minus<Cell>() ):    {-567,-567}
std::reduce(vector1.begin(), 6, Cell{0, 0} std::minus<Cell>() ):    {-683,-683}
std::reduce(vector1.begin(), 7, Cell{0, 0} std::minus<Cell>() ):    {-800,-800}std::reduce(vector2.begin(), 0, Cell{1, 1} std::multiplies<Cell>() ):    {1,1}
std::reduce(vector2.begin(), 1, Cell{1, 1} std::multiplies<Cell>() ):    {110,110}
std::reduce(vector2.begin(), 2, Cell{1, 1} std::multiplies<Cell>() ):    {12320,12320}
std::reduce(vector2.begin(), 3, Cell{1, 1} std::multiplies<Cell>() ):    {1379840,1379840}
std::reduce(vector2.begin(), 4, Cell{1, 1} std::multiplies<Cell>() ):    {157301760,157301760}
std::reduce(vector2.begin(), 5, Cell{1, 1} std::multiplies<Cell>() ):    {1224436736,1224436736}
std::reduce(vector2.begin(), 6, Cell{1, 1} std::multiplies<Cell>() ):    {1525177344,1525177344}
std::reduce(vector2.begin(), 7, Cell{1, 1} std::multiplies<Cell>() ):    {1107477504,1107477504}std::reduce(vector2.begin(), 0, Cell{1024, 1024} std::divides<Cell>() ):    {1024,1024}
std::reduce(vector2.begin(), 1, Cell{1024, 1024} std::divides<Cell>() ):    {9,9}
std::reduce(vector2.begin(), 2, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}
std::reduce(vector2.begin(), 3, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}
std::reduce(vector2.begin(), 4, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}
std::reduce(vector2.begin(), 5, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}
std::reduce(vector2.begin(), 6, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}
std::reduce(vector2.begin(), 7, Cell{1024, 1024} std::divides<Cell>() ):    {0,0}std::reduce(vector2.begin(), 0, Cell{1024, 1024} std::modulus<Cell>() ):    {1024,1024}
std::reduce(vector2.begin(), 1, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 2, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 3, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 4, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 5, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 6, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}
std::reduce(vector2.begin(), 7, Cell{1024, 1024} std::modulus<Cell>() ):    {34,34}


http://www.ppmy.cn/devtools/95141.html

相关文章

后端Web之分层解耦(控制反转IOC-依赖注入DI)

目录 1.三层架构 2.IOC-DI引入 3.IOC-DI使用 4.IOC细节 5.DI细节 内聚&#xff08;Cohesion&#xff09;和耦合&#xff08;Coupling&#xff09;是软件工程中两个重要的概念&#xff0c;它们衡量了软件组件的组织方式和组件之间的相互依赖程度。高内聚性意味着模块内的元…

OSI 七层网络模型和 TCP/IP 四层网络模型

OSI 七层网络模型 网络的七层架构从下到上主要分为&#xff1a;物理层、数据链路层、网络层、传输层、会话层、表示层和应用层 物理层主要定义物理设备标准&#xff0c;它的主要作用是传输比特流&#xff0c;具体做法是在发送端将 1、0 码转化为电流强弱来进行传输&#xff0…

Linux内核编程(十二)热插拔

本文目录 一、知识点1. 热插拔概念2. 热插拔机制3. Netlink机制 二、内核发送uevent事件到用户空间1. kobject发送uevent事件2. udevadm命令查看★示例代码&#xff1a;★优化&#xff1a;完善kset_uevent_ops&#xff08;热插拔事件结构体&#xff09; 三、用户空间使用Netlin…

TypeError: Cannot read properties of undefined (reading ‘scrollIntoView‘)(已解决)

问题复现&#xff1a;眨眼睛使用vitevue3实现跳转dom功能时使用了scrollIntoView方法&#xff0c;在打包上传以后使用该功能报错 小友可能会陷入误区&#xff0c;以为是函数方法有问题&#xff0c;毕竟在开发时是没有问题的&#xff0c; 而实际上呢问题出在获取节点失败了 在这…

使用 Spring Boot 实现职责链模式处理电商订单流程

业务场景 假设我们正在构建一个电商系统&#xff0c;需要处理用户的订单请求。当用户提交订单时&#xff0c;系统需要执行一系列检查和操作&#xff0c;比如验证库存、检查用户信用额度、确认支付方式等。如果所有检查都通过&#xff0c;则订单被创建&#xff1b;否则&#xf…

五个方法帮你克服拖延症,停止内耗!

有人希望我们能出一期干货文&#xff0c;教大家如何克服拖延症&#xff1f;这不&#xff1f;有求必应的我们火速为大家安利几个小诀窍&#xff0c;帮助你停止内耗&#xff0c;告别拖延&#xff01; 方法一、根据DDL安排时间表 “DDL是第一生产力”这句话简直是拖延患者的口头…

【FreeRTOS】队列实验多设备玩游戏(思路)

目录 0 前言1. 队列实验_多设备玩游戏2 代码分析2.1 讲解2.1.1 球的任务2.1.2 挡球板的任务 2.2 程序改进思路 0 前言 学习视频&#xff1a; 【FreeRTOS入门与工程实践 --由浅入深带你学习FreeRTOS&#xff08;FreeRTOS教程 基于STM32&#xff0c;以实际项目为导向&#xff09…