tchapter11-32.cpp

来自「STL程序员开发指南源码」· C++ 代码 · 共 31 行

CPP
31
字号
//文件名:CHAPTER11-32.cpp
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
typedef vector < float > FloatArray;
typedef ostream_iterator < float, char, char_traits<char> >
FloatOstreamIt;
void main ()
{
    FloatOstreamIt itOstream(cout," ");
    FloatArray rgF1, rgF2;    // Initialize the arrays
    for (int i=1; i<=5; i++) {        rgF1.push_back(i);        rgF2.push_back(i*i);    };
    cout << "Array 1: ";     // Print the arrays
    copy(rgF1.begin(),rgF1.end(),itOstream);
    cout << endl << "Array 2: ";
    copy(rgF2.begin(),rgF2.end(),itOstream);
    cout << endl;
    // Compute the inner_product of the arrays.  This is the
    // sum of the products (S.O.P) of the corresponding elements
    float ip1 = inner_product(rgF1.begin(),rgF1.end(),rgF2.begin(),0);
    cout << "The inner product (S.O.P) of Array1 and Array2 is " << ip1 << endl;
    // Compute the inner_product of the arrays.  This is the
    // product of the sums (P.O.S.) of the corresponding elements
    float ip2 = inner_product(rgF1.begin(),rgF1.end(),rgF2.begin(),1,
 multiplies<float>(),plus<float>());
    cout << "The inner product (P.O.S.) of Array1 and Array2 is " << ip2 << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?