++


inner_product()


template< class InputIterator1, class InputIterator2

class Type >

Type

inner_product(

InputIterator1 first1, InputIterator1 last,

InputIterator2 first2, Type init );

 

template< class InputIterator1, class InputIterator2

class Type,

class BinaryOperation1, class BinaryOperation2 >

Type

inner_product(

InputIterator1 first1, InputIterator1 last,

InputIterator2 first2, Type init,

BinaryOperation1 op1, BinaryOperation2 op2 );

init. [first1,last1), first2 . , {2,3,5,8} {1,2,3,4}, :

2*1 + 3*2 + 5*3 + 8*4

0, 55.

op1,   op1. , op1 op2, :

(2+1) - (3+2) - (5+3) - (8+4)

inner_product()  . <numeric>.

#include <numeric>

#include <vector>

#include <iostream.h>

int main()

{

int ia[] = { 2, 3, 5, 8 };

int ia2[] = { 1, 2, 3, 4 };

 

// ,

// : 0

int res = inner_product( &ia[0], &ia[4], &ia2[0], 0 );

 

// : : 55

cout << " : "

<< res << endl;

vector<int, allocator> vec( ia, ia+4 );

vector<int, allocator> vec2( ia2, ia2+4 );

 

// ,

// : 0

res = inner_product( vec.begin(), vec.end(),

vec2.begin(), 0,

minus<int>(), plus<int>() );

 

// : : -28

cout << " : "

<< res << endl;

return 0;

}




- -  - -  - -