C++ STD accumulate函数
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函数的更多相关文章
- #417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)
题目链接 : http://codeforces.com/problemset/problem/812/C 题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价 ...
- std::accumulate使用的一个小细节
今天使用std::accumulate模板函数的时候出现了一个错误,特此记录一下. #include <iostream> #include <numeric> int mai ...
- c++多线程编程:实现标准库accumulate函数的并行计算版本
今天使用c++实现了标准库头文件<numeric>中的accumulate函数的并行计算版本,代码如下,注释写的比较详细,仅对其中几点进行描述: ①该实现假定不发生任何异常,故没有对可能产 ...
- std::string,std::vector,std::accumulate注意事项
在用string做字符串拼接时,会发现随着string的增大越来越慢,原因主要是string(和vector)是基于现行内存的数据结构,在海量数据时,经常会申请新的一块内存,把原有的数据拷贝过去然后再 ...
- C/C++ 错误笔记-解决swap函数与标准库的std::swap函数冲突的问题
下午写了一份代码: #include <iostream> using namespace std; // 模板1:交换基本类型的值 template<typename T> ...
- 一文带你详细介绍c++中的std::move函数
前言 在探讨c++11中的Move函数前,先介绍两个概念(左值和右值) 左值和右值 首先区分左值和右值 左值是表达式结束后依然存在的持久对象(代表一个在内存中占有确定位置的对象) 右值是表达式结束时不 ...
- C++ STD inner_product函数
C++ STD函数 inner_product是c++标准库封装的一个函数. 函数原型: 函数1: inner_product(beg1, end1, beg2, init) 函数2: inner ...
- 右值引用和std::move函数(c++11)
1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...
- std::function与std::bind 函数指针
function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时. std::function可以绑定到全局函数/类静态成员 ...
随机推荐
- Fluent Validation
.NET业务实体类验证组件Fluent Validation 认识Fluent Vaidation. 看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真 ...
- Spring Resource之内置的Resource实现
Spring提供了大量的并且可以直接使用的Resource实现 1.UrlResource UrlResource封装了一个java.net.URL,而且可以通过一个URL用于访问任何对象,例如文件. ...
- Visual Studio 2015 & C#6.0
Visual Studio 2015 & C#6.0 试用报告,持续更新. 昨天早上看到了.net开源的消息,我是非常兴奋的,毕竟局限于Windows的.NET经常被人唾弃.VB暂且不 ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 大约ActionContext.getContext()使用体验
这是我在另一个人的博客看了,原来博客的时间长一点.我把它简化了一下,运营商,以方便它看起来. 为了避免与Servlet API耦合在一起,方便Action类做单元測试,Struts 2对HttpSer ...
- 12个很少被人知道的CSS事实
之前没有认真的研究过,padding-bottom的值如果是百分比,那么它的实际值是根据父类的宽度来调整的.我还以为是根据这个元素的本身的宽度来定义呢?汗..padding-top/padding-l ...
- Object.prototype.propertyIsEnumerable
语法: obj.propertyIsEnumerable(prop); 此方法返回一个布尔值,表明指定的属性名是否是当前对象可枚举的自身属性. 1.如果是用户自定义了对象的属性,将返回true,比如 ...
- quartz_spring 定时器配置
quartz:石英,表达精确准时的意思. quartz-all-1.6.1.jar 主要用于定时任务管理. <?xml version="1.0" encoding=&quo ...
- 实现栈最小元素的min函数
#include<iostream> #include<stack> using namespace std; class min_stack { public: void p ...
- 使用Entity Framework 4进行代码优先开发
[原文地址]Code-First Development with Entity Framework 4 .NET 4随带发布了一个改进版的Entity Framework(EF)- 一个位于Sy ...