C++11标准模板(STL)- 算法(std::partial_sum)

news2024/10/6 8:22:49
定义于头文件  <numeric>

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

计算范围内元素的部分和

std::partial_sum

template< class InputIt, class OutputIt >
OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first );

(1)
template< class InputIt, class OutputIt, class BinaryOperation >

OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first,

                      BinaryOperation op );
(2)

计算范围 [first, last) 的子范围中元素的部分和,并写入到始于 d_first 的范围。第一版本用 operator+ ,第二版本用给定的二元函数 op 对元素求和,均将 std::move 应用到其左侧运算数 (C++20 起)。

等价运算:

*(d_first)   = *first;
*(d_first+1) = *first + *(first+1);
*(d_first+2) = *first + *(first+1) + *(first+2);
*(d_first+3) = *first + *(first+1) + *(first+2) + *(first+3);
...

 

op 必须无副效应。

(C++11 前)

op 必须不非法化涉及范围的任何迭代器,含尾迭代器,或修改该范围的任何元素。

(C++11 起)

参数

first, last-要求和的元素范围
d_first-目标范围起始;可以等于 first
op-被使用的二元函数对象。

该函数的签名应当等价于:

 Ret fun(const Type1 &a, const Type2 &b);

签名中并不需要有 const &。
类型 Type1 必须使得 iterator_traits<InputIt>::value_type 类型的对象能隐式转换到 Type1 。类型 Type2 必须使得 InputIt 类型的对象能在解引用后隐式转换到 Type2 。 类型 Ret 必须使得 iterator_traits<InputIt>::value_type 类型对象能被赋 Ret 类型值。 ​

类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。

返回值

指向最后被写入元素后一元素的迭代器。

复杂度

恰好应用 (last - first) - 1 次二元运算

可能的实现

版本一

template<class InputIt, class OutputIt>
OutputIt partial_sum(InputIt first, InputIt last, 
                     OutputIt d_first)
{
    if (first == last) return d_first;
 
    typename std::iterator_traits<InputIt>::value_type sum = *first;
    *d_first = sum;
 
    while (++first != last) {
       sum = std::move(sum) + *first; // C++20 起有 std::move
       *++d_first = sum;
    }
    return ++d_first;
 
    // 或 C++14 起:
    // return std::partial_sum(first, last, d_first, std::plus<>());
}

版本二

template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt partial_sum(InputIt first, InputIt last, 
                     OutputIt d_first, BinaryOperation op)
{
    if (first == last) return d_first;
 
    typename std::iterator_traits<InputIt>::value_type sum = *first;
    *d_first = sum;
 
    while (++first != last) {
       sum = op(std::move(sum), *first); // C++20 起有 std::move
       *++d_first = sum;
    }
    return ++d_first;
}

调用示例

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <iterator>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    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
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::mt19937 g{std::random_device{}()};

    srand((unsigned)time(NULL));;

    std::cout << std::boolalpha;

    std::function<Cell()> generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    // 初始化lCells1
    std::list<vector<Cell>> lCells1(5, vector<Cell>(5));
    //用从起始值开始连续递增的值填充一个范围
    for (vector<Cell> &vCells : lCells1)
    {
        std::iota(vCells.begin(), vCells.end(), Cell(generate()));
    }

    size_t index = 0;
    for (vector<Cell> &vCells : lCells1)
    {
        std::cout << "cell  " << index << "       ";
        std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::vector<Cell> pCells(vCells.size());
        //计算范围 [first, last) 的子范围中元素的部分和,并写入到始于 d_first 的范围。
        //第一版本用 operator+
        std::partial_sum(vCells.begin(), vCells.end(), pCells.begin());
        std::cout << "partial_sum   ";
        std::copy(pCells.begin(), pCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        index ++;
    }

    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;


    auto opt = [](const Cell & a, const Cell & b)
    {
        Cell cell{a.x + b.x, a.y + b.y};
        return cell;
    };

    // 初始化lCells2
    std::list<vector<Cell>> lCells2(5, vector<Cell>(5));
    //用从起始值开始连续递增的值填充一个范围
    for (vector<Cell> &vCells : lCells2)
    {
        std::iota(vCells.begin(), vCells.end(), Cell(generate()));
    }

    index = 0;
    for (vector<Cell> &vCells : lCells2)
    {
        std::cout << "cell  " << index << "       ";
        std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::vector<Cell> pCells(vCells.size());
        //计算范围 [first, last) 的子范围中元素的部分和,并写入到始于 d_first 的范围。
        //第二版本用给定的二元函数 op 对元素求和
        std::partial_sum(vCells.begin(), vCells.end(), pCells.begin(), opt);
        std::cout << "partial_sum   ";
        std::copy(pCells.begin(), pCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        index ++;
    }

    return 0;
}

输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/125723.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

达达盈利新故事,得靠智能化“省”出来?

&#xff08;图片来源于网络&#xff0c;侵删&#xff09; 文|螳螂观察 作者|叶小安 我们正处于一个最好的时代&#xff0c;不用出门就能享受到叫餐、代买衣物服饰、收发快递甚至是医院排队等服务&#xff0c;“万物皆可到家、万物即到”&#xff0c;正成为时代的潮流。 不…

【信管5.4】进度管理知识点汇总

进度管理知识点汇总在项目进度管理这一块&#xff0c;我们突然一下就接触到了不少的计算操作&#xff0c;而且接触到的工具概念也相比范围管理来说多了很多。因此&#xff0c;我们在这里进行一次小的总结。活动顺序与逻辑关系还记得什么是活动吧&#xff1f;活动就是 WBS 再次分…

NFS And Autofs

NFS&#xff08;network file system&#xff09; 挂载NFS 文件系统。NFS是一个标准的网络协议用在linux与unix之间,版本7默认使用的是NFSV4&#xff0c;NFSv4使用TCP协议&#xff0c;旧版本的NFS使用TCP或者UDP协议。 *手动挂载NFS使用mount. *自动挂载使用/etc/fstab *挂载NF…

FFmpeg常用推流命令

一、FFmpeg推RTMP流准备工作 首先确保自已已经安装了nginx rtmp服务器。 打开配置文件nginx.conf 完成如下配置 如果没有nginx rtmp服务器&#xff0c;请阅读这一篇简书文章 Mac搭建nginxrtmp服务器 二、FFmpeg推流 1.推流MP4文件 视频文件地址&#xff1a;/Users/xu/Desk…

美创DSM数据安全管理平台获华为鲲鹏技术认证!

近期&#xff0c;美创DSM数据安全管理平台通过华为鲲鹏的相互兼容性测试与认证&#xff0c;这标志着数据安全管理平台对国产信创服务器的支持&#xff0c;实现自研、国产化和自主可控。 华为鲲鹏技术认证是华为推出的一项生态合作伙伴计划&#xff0c;要求测试产品自主、可控…

健康体检管理系统源码 运营级PEIS系统源码 PEIS健康体检系统源码 PEIS源码 B/S架构开发

开发语言:ASP.NET C#,数据库:SQLserver2008R2&#xff0c;开发工具:VS2010。 前台工作&#xff1a; 预约、前台登记、照片采集、导检单打印、检验申请单打印、检前签到、检后签到、 团体设置、合并团体&#xff08;逻辑&#xff09; 医生工作&#xff1a; 数据集中录入、数…

国产linux系统使用 PageOffice 在线打开 word 文件

一、客户端环境 1、操作系统 银河麒麟&#xff0c;中标麒麟&#xff0c;统信UOS 2、芯片 芯片&#xff08;CPU)&#xff1a;x86&#xff08;Intel、兆芯&#xff09; &#xff0c;ARM&#xff08;飞腾、鲲鹏&#xff09;&#xff0c;龙芯 3、浏览器 360安全浏览器 奇安信…

NNOM神经网络语音降噪

目录 1. 问题记录和解决 2. C工程建立和运行 1. 问题记录和解决 &#xff08;1&#xff09;python语音处理依赖库soundfile 在miniconda的powershell中执行指令&#xff1a; pip install soundfile –i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com &a…

js函数篇

函数声明提升 面试题&#xff1a;先提升函数&#xff0c;再声明变量提升 arguments function fun(){var sum0;for(var i0;i<arguments.length;i){sumarguments[i];}console.log(所有参数的和是sum); } fun(33,44,23,34); 函数算法面试题 1.喇叭花数 abc a! b! c! fun…

使用WxJava快速接入微信公众号

在微信公众号请求用户网页授权之前&#xff0c;开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中&#xff0c;修改授权回调域名。 环境准备 1.申请公众号测试账号2.外网服务准备以及配置3.常用开发工具及网站…

为什么四天工作制才是企业良药,而非裁员?原因你想不到

经济危机&#xff0c;疫情影响&#xff0c;裁员不是企业良药&#xff0c;四天工作制或许才是长远之道。四天工作制&#xff0c;一周工作四天&#xff0c;你愿意吗&#xff1f;能救企业吗&#xff1f;能减少裁员吗&#xff1f;回答这些问题&#xff0c;我们需要了解五天工作制的…

MySQL-MySQL知识点总结

1、MySQL介绍 MySQL是一种关系型数据库&#xff0c;主要用于持久化存储数据。 2、MySQL基础架构 &#xff08;1&#xff09;组成 客户端、Server层和存储引擎层。 &#xff08;2&#xff09;主要构成部分 连接器&#xff1a;身份认证和权限验证。 查询缓存&#xff1a;执行查询…

【Django】第三课 基于Django图书借阅管理网站平台

概念 本文在上一篇文章之上&#xff0c;完成借阅图书功能&#xff0c;查看借阅记录功能&#xff0c;归还图书&#xff0c;查看历史借阅记录&#xff0c;删除历史借阅记录等等 借阅图书功能实现 当前学生查阅图书的时候&#xff0c;如果当前学生没有借阅过该书&#xff0c;或…

H5U PLC本地脉冲轴和本地编码器轴测试

H5U PLC如何通过EtherCAT总线控制伺服运动,请参看下面的博客 汇川H5U PLC通过EtherCAT总线控制SV660N和X3E伺服_RXXW_Dor的博客-CSDN博客首先我们看下系统硬件和软件配置:汇川H5U PLC的编程软件是:AutoShop V4.6.3.0 硬件:PLC H5U-1614MTD-A16,汇川伺服型号:SV660NS1R6I…

nacos看这一篇文章就够了

第一章 nacos简介 1 2 3 4 5 6 7 8Nacos主要帮助我们发现、注册、配置和管理微服务1. 注册中心功能(Dubbo或者SpringCloud介绍) 2. 配置中心功能 3. 服务管理功能(Dubbo或者SpringCloud介绍)官网地址: https://nacos.io/zh-cn/index.html第二章 nacos环境 1 2 3 4 51. nacos 依…

【HTML】再见2022!一起来写一个响应式跨年倒计时吧!(附源码)

&#x1f482;作者简介&#xff1a; THUNDER王&#xff0c;一名热爱财税和SAP ABAP编程以及热爱分享的博主。目前于江西师范大学会计学专业大二本科在读&#xff0c;同时任汉硕云&#xff08;广东&#xff09;科技有限公司ABAP开发顾问。在学习工作中&#xff0c;我通常使用偏后…

【信息学CSP-J近16年历年真题64题】真题练习与解析 第10题之公交换乘

描述 著名旅游城市 B 市为了鼓励大家采用公共交通方式出行,推出了一种地铁换乘公交 车的优惠方案: 在搭乘一次地铁后可以获得一张优惠票,有效期为 45 分钟,在有效期内可以 消耗这张优惠票,免费搭乘一次票价不超过地铁票价的公交车。在有效期内指 开始乘公交车的时间与开…

结合商业项目深入理解Go知识点

这篇文章比较硬核&#xff0c;爆肝5千字&#xff0c;把之前整理的知识点都串起来了。建议先收藏&#xff0c;慢慢看。 前言 上一篇文章 #【Go WEB进阶实战】开源的电商前后台API系统 很受大家欢迎&#xff0c;有好多小伙伴私信我问题&#xff1a;“gtoken真不错&#xff0c;能…

【SpringBoot应用篇】SpringBoot使用Aspect AOP注解实现日志管理(增强版)

【SpringBoot应用篇】SpringBoot使用Aspect AOP注解实现日志管理&#xff08;增强版&#xff09;pomLog实体类OperateLogOrderGoodLogAspect转换器ConvertGoodConvertOrderConvertAopController启动类EnableAutoOperateLog需求: 需要保存的日志内容在方法的参数中&#xff0c;并…

Elasticsearch 谷歌插件 Elasticsearch-head 使用

目录 什么是Elasticsearch-head 安装 ​编辑界面 ​编辑集群健康值的几种状态如下 解决跨域问题 基本使用 创建索引 点击概览 点击数据浏览 什么是Elasticsearch-head ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎&#x…