1. 介绍

  用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果。其头文件在numeric中。

  用次函数可以求和,构造前n项和的向量,乘积,构造前n项乘积的向量

2. 应用举例

#include <vector>
#include <numeric>
#include <functional>
#include <iostream> using namespace std; int main( )
{ vector <int> v1, v2( 20 );
vector <int>::iterator Iter1, Iter2; int i;
for ( i = 1 ; i < 21 ; i++ )
{
v1.push_back( i );
} cout << "最初向量v1中个元素的值为:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl; // accumulate函数的第一个功能,求和
int total;
total = accumulate ( v1.begin ( ) , v1.end ( ) , 0 ); cout << "整数从1到20的和为: "
<< total << "." << endl; // 构造一个前n项和的向量
int j = 0, partotal;
for ( Iter1 = v1.begin( ) + 1; Iter1 != v1.end( ) + 1 ; Iter1++ )
{
partotal = accumulate ( v1.begin ( ) , Iter1 , 0 );
v2 [ j ] = partotal;
j++;
} cout << "前n项和分别为:\n ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl << endl; // accumulate函数的第二个功能,计算连乘积
vector <int> v3, v4( 10 );
vector <int>::iterator Iter3, Iter4; int s;
for ( s = 1 ; s < 11 ; s++ )
{
v3.push_back( s );
} cout << "向量v3的初始值分别为:\n ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl; int ptotal;
ptotal = accumulate ( v3.begin ( ) , v3.end ( ) , 1 , multiplies<int>( ) ); cout << "整数1到10的连乘积为: "
<< ptotal << "." << endl; // 构造一个前n项积的向量
int k = 0, ppartotal;
for ( Iter3 = v3.begin( ) + 1; Iter3 != v3.end( ) + 1 ; Iter3++ ) {
ppartotal = accumulate ( v3.begin ( ) , Iter3 , 1 , multiplies<int>( ) );
v4 [ k ] = ppartotal;
k++;
} cout << "前n项积分别为:\n ( " ;
for ( Iter4 = v4.begin( ) ; Iter4 != v4.end( ) ; Iter4++ )
cout << *Iter4 << " ";
cout << ")." << endl;
}

编译运行,看一下输出结果: 

 
 

C++ STD accumulate函数的更多相关文章

  1. #417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)

    题目链接 : http://codeforces.com/problemset/problem/812/C 题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价 ...

  2. std::accumulate使用的一个小细节

    今天使用std::accumulate模板函数的时候出现了一个错误,特此记录一下. #include <iostream> #include <numeric> int mai ...

  3. c++多线程编程:实现标准库accumulate函数的并行计算版本

    今天使用c++实现了标准库头文件<numeric>中的accumulate函数的并行计算版本,代码如下,注释写的比较详细,仅对其中几点进行描述: ①该实现假定不发生任何异常,故没有对可能产 ...

  4. std::string,std::vector,std::accumulate注意事项

    在用string做字符串拼接时,会发现随着string的增大越来越慢,原因主要是string(和vector)是基于现行内存的数据结构,在海量数据时,经常会申请新的一块内存,把原有的数据拷贝过去然后再 ...

  5. C/C++ 错误笔记-解决swap函数与标准库的std::swap函数冲突的问题

    下午写了一份代码: #include <iostream> using namespace std; // 模板1:交换基本类型的值 template<typename T> ...

  6. 一文带你详细介绍c++中的std::move函数

    前言 在探讨c++11中的Move函数前,先介绍两个概念(左值和右值) 左值和右值 首先区分左值和右值 左值是表达式结束后依然存在的持久对象(代表一个在内存中占有确定位置的对象) 右值是表达式结束时不 ...

  7. C++ STD inner_product函数

    C++ STD函数   inner_product是c++标准库封装的一个函数. 函数原型: 函数1: inner_product(beg1, end1, beg2, init) 函数2: inner ...

  8. 右值引用和std::move函数(c++11)

    1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...

  9. std::function与std::bind 函数指针

    function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时. std::function可以绑定到全局函数/类静态成员 ...

随机推荐

  1. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  2. 探秘ReSharper8.1版本中Architecture(架构工具)的改进

    在ReSharper 8.0新版本中,有一个叫做Architecture(结构工具)的新功能,此功能被定义为项目依赖关系分析.其目的是让用户可视化操作解决方案的结构.接下来,小编将在ReSharper ...

  3. 远程控制编写之屏幕传输 MFC实现 屏幕截图 发送bmp数据 显示bmp图像

    远程控制编写之屏幕传输  MFC实现  屏幕截图 发送bmp数据 显示bmp图像: 一 : 首先要了解bmp图像的结构 详情请看我转载的一篇文章http://blog.csdn.net/hnust_x ...

  4. SQL远程备份

    原文:SQL远程备份 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go   -- ====================================== ...

  5. IQueryable与IQEnumberable的区别

    IEnumberable接口: 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用froeach遍历此object; IQueryable接口 ...

  6. 实现透明渐变的Activity

    如果光是透明全屏的Activity的话,直接继承内置theme即可 <activity android:theme="@android:style/Theme.NoTitleBar.F ...

  7. Http Pipeline详细分析(下)

    Http Pipeline详细分析(下) 文章内容 接上面的章节,我们这篇要讲解的是Pipeline是执行的各种事件,我们知道,在自定义的HttpModule的Init方法里,我们可以添加自己的事件, ...

  8. C#中的深复制与浅复制

    C#中分为值类型和引用类型,值类型的变量直接包含其数据,而引用类型的变量则存储对象的引用. 对于值类型,每个变量都有自己的数据副本,对一个变量的操作不可能影响到另一个变量.如 class Progra ...

  9. DLL文件的引用

    首先我们先要写一个DLL文件: 我先创建一个win32的DLL工程,在工程中添加了Math.h和Math.cpp文件,具体内容如下: Math.h: #pragma once #include &qu ...

  10. SQL Server中tempdb的management

    对<SQL Server中tempdb的management>的一些更正和补充   对<SQL Server中tempdb的management>的一些更正和补充 前几天看了这 ...