C++ vector计算数组之和

    在C++ vector是一个动态数组,支持按下标索引访问、顺序访问、动态扩容等。计算vector里的元素之和,既可以通过for循环遍历每一个元素,然后相加得到数组之和;也可以通过调用accumulate()库函数,输入vector的起点、终点、参考原点(默认是0或者0.000),来得到数组之和;还可以通过for_each+lamba表达式,来计算元素之和。

    比如,有一个数组std::vector arry = { 1,2,3,4,5,6,7,8,9 }, 计算这个arry数组之和。

1 for循环计算数组之和

    在for循环中,定义一个局部变量total1,保存每次的累加和。

cpp 复制代码
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> arry = { 1,2,3,4,5,6,7,8,9 };

    //方法一,for循环计算数组之和
    int total1 = 0;
    for(auto item: arry)
    {
        total1 += item;        
    }
    std::cout << "#1 total1= " << total1 << std::endl;
	return 0;
}

2 accumulate计算数组之和

    accumulate()库函数,其头文件是#include <numeric>, 它既可以计算整数之和,也可以计算浮点数之和,还可以打印字符串。它有4个模版函数,如下

cpp 复制代码
#include <numeric>

// 1)计算数组之和, 数值型
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );

// 2)计算数组之和,字符型
template< class InputIt, class T >
constexpr T accumulate( InputIt first, InputIt last, T init );

// 3) 按BinaryOperation方式, 操作数值数组元素
template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init,
              BinaryOperation op );
    
// 4) 按BinaryOperation方式, 操作字符数组元素     
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init,
                        BinaryOperation op );

    由于是计算数值型数组之和,这里选择第一个模版函数,它的第三个参数表示值的类型和参考值,对于int,默认是0,对于double,默认是0.000。

cpp 复制代码
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );

    完整代码如下:

cpp 复制代码
#include <iostream>
#include <vector>
#include <numeric>

int main()
{
    std::vector<int> arry = { 1,2,3,4,5,6,7,8,9 };

    //方法二,accumulate计算数组之和
    int total2 = accumulate(arry.begin(), arry.end(), 0);
    std::cout << "#2 total2= " << total2 << std::endl;

    return 0;
}

3 for_each计算数组之和

    for_each计算数组之和,使用了lamba表达式,局部变量total3按引用传递给lamba,保存累加之后的值,它的模版函数 如下:

cpp 复制代码
#include <algorithm>

template< class InputIt, class UnaryFunction >
UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

    完整代码如下:

cpp 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> arry = { 1,2,3,4,5,6,7,8,9 };

    //方法三,for_each计算数组之和
    int total3 = 0;
    std::for_each(std::begin(arry), std::end(arry), [&total3](auto arry) {total3 += arry; });
    std::cout << "#3 total3= " << total3  << std::endl;

    return 0;
}

参考文献

    【1】std::accumulate cppreference.com

    【2】std::for_each cppreference.com

相关推荐
青柠之夏cc19 分钟前
前端拖拽功能原生实现,不引入拖拽库完成业务
开发语言·前端·python
ym hyd 11126 分钟前
题库建立与考试系统源码 Java+SpringBoot+Vue3 前后分离
java·开发语言·vue.js·spring boot·毕设
OKkankan32 分钟前
Python 高阶语法(一):高阶函数、闭包、lambda 与 functools——从底层真正理解装饰器
开发语言·python
geovindu39 分钟前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
福大大架构师每日一题1 小时前
Rust 1.99.0发布:C 可变参数、裸函数、Cargo 配置、Rustdoc 性能与大量兼容性调整全解析
c语言·开发语言·rust
智鸟科技GemeOpen开发者智能设备1 小时前
平安校园 AI 智能实时告警系统 智鸟科技·GemeOpen + 谷华科技·融合创新方案白皮书
java·开发语言·python·物联网·智能家居
Wang's Blog1 小时前
Java框架 SpringCloud 快速入门: Feign 的自定义配置与日志级别
java·开发语言·spring cloud
成旭先生1 小时前
企业全景信息查询 API:工商照面 33 项、股东出资、变更与社保一次查全
java·开发语言·api接口·企业信息查询·工商数据·风控尽调·供应商准入
2601_962885721 小时前
批量取数比循环单取快多少?用 AlphaFeed klines.batch给多股票取数提速
开发语言·php·batch
动词ing2 小时前
【C语言题目练习】算法 整数反转
c语言·开发语言·算法