accumulate function of C + + STL algorithm

Source: Internet
Author: User

1. Introduction

In addition to calculating all the elements of a specific range, including successive parts and initial values, you can also use the specified binary operation to calculate the result of an element within a particular range. Its header file is in numeric.
The accumulate original function declaration is defined as follows:
  

template<classclassType>   Type accumulate(      InputIterator _First,       InputIterator _Last,       Type _Val   );template<classclassTypeclass Fn2>   Type accumulate(      InputIterator _First,       InputIterator _Last,       Type _Val,       BinaryOperation _Binary_op   );

Parameter description.

Parameters Description
_first Specifies the value of the first iteration in the range or is used with the action option.
Inputiterator _last The last iteration value in the specified range or in conjunction with an action item.
_val The initial value to evaluate.
_binary_op Applies to all the elements in the specified range and to the parameters that were previously computed for the result.

But it may not be easy to read the instructions, we can explain this function more concretely by example.

2. Application examples
#include <vector>#include <numeric>#include <functional>#include <iostream>using namespace STD;intMain () { vector <int>V1, v2 ( -); vector <int>:: Iterator Iter1, Iter2;intI for(i =1; I < +;   i++) {v1.push_back (i); }cout<<"The value of the first element in the vector v1 is: \ n ("; for(Iter1 = V1.begin (); Iter1! = V1.end (); iter1++)cout<< *iter1 <<" ";cout<<")."<< Endl;the first function of the//accumulate function, sum   intTotal Total = Accumulate (V1.begin (), V1.end (),0);cout<<"integers from 1 to 20 and for:"<< Total <<"."<< Endl;//construct a vector of the first n items   intj =0, Partotal; for(Iter1 = V1.begin () +1; Iter1! = V1.end () +1; iter1++) {partotal = accumulate (V1.begin (), Iter1,0);      V2 [j] = Partotal;   j + +; }cout<<the first n and respectively: \ n ("; for(Iter2 = V2.begin (); Iter2! = V2.end (); iter2++)cout<< *iter2 <<" ";cout<<")."<< Endl << Endl;the second function of the//accumulate function, which computes the product of a continuous    vector <int>V3, v4 (Ten); vector <int>:: Iterator Iter3, Iter4;intS for(s =1; s < One;   s++) {v3.push_back (s); }cout<<The initial values for the vector v3 are: \ n ("; for(Iter3 = V3.begin (); Iter3! = V3.end (); iter3++)cout<< *iter3 <<" ";cout<<")."<< Endl;intPtotal; Ptotal = Accumulate (V3.begin (), V3.end (),1, multiplies<int> ());cout<<"The multiplication of integers 1 to 10 is:"<< Ptotal <<"."<< Endl;//construct a vector of the first n product   intK =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<<"The first n product is: \ n ("; for(Iter4 = V4.begin (); Iter4! = V4.end (); iter4++)cout<< *iter4 <<" ";cout<<")."<< Endl;}

Compile and run and look at the output:

accumulate function of C + + STL algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.