Freigeben über


inner_product (STL Samples)

Veranschaulicht, wie die Funktion inner_product Standardvorlagenbibliothek (STL) in Visual C++ verwendet.

template<class InputIterator1, class InputIterator2, class T>
   inline T inner_product(
      InputIterator First,
      InputIterator Last,
      InputIterator First2,
      T Init
   )
template<
   class InputIterator1,
   class InputIterator2,
   class T,
   class BinOp1,
   class BinOp2
>
   inline T inner_product(
      InputIterator1 First,
      InputIterator1 Last,
      InputIterator2 First2,
      T Init,
      BinOp1 Binary_Op1,
      BinOp2 Binary_Op2
   )

Hinweise

HinweisHinweis

Die Klasse/Parameternamen im Prototyp stimmen nicht mit der Version in der Headerdatei ab.Einige wurden geändert, um die Lesbarkeit zu verbessern.

inner_product sein Ergebnis berechnet, indem der Akkumulator Init initialisiert und dann mit acc mit ändert: acc = acc + (*i1 (*) *i2) - oder - acc = Binary_Op1(acc, Binary_Op2(*i1, *i2)) i1Iterator für jeden im Bereich [First, Last) und Iterator i2im Bereich [First2, First2 + (Last - First)) nacheinander.

Beispiel

// inner_product.cpp
// compile with: /EHsc
//
// Description of
//         inner_product(first,last,first2,init)
//         inner_product(first,last,first2,init,binary_op1,binary_op2):
//
//    Computes its result by initializing the accumulator acc with init
//        acc = init
//    and then modifying it with
//        acc = acc  +  (*i1) * (*i2)
//    or
//        acc = binary_op1(acc, binary_op2(*i1, *i2))
//    for every iterator i1 in the range [first, last) and
//    iterator  i2  in  the  range [first2, first2 + (last - first))
//    in order.
///////////////////////////////////////////////////////////////////////

#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>

using namespace std;


typedef vector < int > intArray;
typedef ostream_iterator < int, char, char_traits<char> >
FloatOstreamIt;

int main ()
{
    FloatOstreamIt itOstream(cout," ");

    // Initialize the arrays
    intArray rgF1, rgF2;
    for (int i=1; i<=5; i++) {
        rgF1.push_back(i);
        rgF2.push_back(i*i);
    };

    // Print the arrays

    cout << "Array 1: ";
    copy(rgF1.begin(),rgF1.end(),itOstream);
    cout << endl;
    cout << "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

    int 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
    int ip2 = inner_product(rgF1.begin(),rgF1.end(),rgF2.begin(),1,
                            multiplies<int>(),plus<int>());
    cout << "The inner product (P.O.S.) of Array1 and Array2 is "
       << ip2
       << endl;

}

Output

Array 1: 1 2 3 4 5 
Array 2: 1 4 9 16 25 
The inner product (S.O.P) of Array1 and Array2 is 225
The inner product (P.O.S.) of Array1 and Array2 is 86400

Anforderungen

Header: <numeric>

Siehe auch

Konzepte

Standardvorlagenbibliotheks-Beispiele