📄 inn_8576.htm
字号:
<HTML><TITLE>inner_product</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>inner_product</H2>
<HR><PRE> Generalized Numeric Operation</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Computes the inner product <SAMP>A X B</SAMP> of two ranges <SAMP>A</SAMP> and <SAMP>B</SAMP>.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <numeric>
template <class InputIterator1, class InputIterator2,</PRE>
<PRE> class T>
T <B>inner_product</B> (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);</PRE><PRE>template <class InputIterator1, class InputIterator2,
class T,
class BinaryOperation1,
class BinaryOperation2>
T <B>inner_product</B> (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>There are two versions of <B><I>inner_product</B></I>. The first computes an inner product using the default multiplication and addition operators, while the second allows you to specify binary operations to use in place of the default operations. </P>
<P>The first version of the function computes its result by initializing the accumulator <SAMP>acc</SAMP> with the initial value <SAMP>init</SAMP> and then modifying it with:</P>
<PRE> acc = acc + ((*i1) * (*i2))</PRE>
<PRE></PRE><P>for every iterator <SAMP>i1</SAMP> in the range <SAMP>[first1, last1)</SAMP> and iterator <SAMP>i2</SAMP> in the range <SAMP>[first2, first2 + (last1 - first1))</SAMP>. The algorithm returns <SAMP>acc</SAMP>.</P>
<P>The second version of the function initializes <SAMP>acc</SAMP> with <SAMP>init</SAMP>, then computes the result:</P>
<PRE>acc = binary_op1(acc, binary_op2(*i1, *i2))</PRE>
<PRE></PRE><P>for every iterator <SAMP>i1</SAMP> in the range <SAMP>[first1, last1)</SAMP> and iterator <SAMP>i2</SAMP> in the range <SAMP>[first2, first2 + (last1 - first1))</SAMP>.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>The <B><I>inner_product</B></I> algorithm computes exactly <SAMP>(last1 - first1)</SAMP> applications of either:</P>
<PRE>acc + (*i1) * (*i2)</PRE>
<P> or </P>
<PRE>binary_op1(acc, binary_op2(*i1, *i2)).</PRE>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// inr_prod.cpp
//
#include <numeric> //For inner_product
#include <list> //For list
#include <vector> //For vectors
#include <functional> //For plus and minus
#include <iostream.h></PRE>
<PRE> int main()
{
//Initialize a list and an int using arrays of ints
int a1[3] = {6, -3, -2};
int a2[3] = {-2, -3, -2};</PRE>
<PRE> list<int> l(a1, a1+3);
vector<int> v(a2, a2+3);</PRE>
<PRE> //Calculate the inner product of the two sets of values
int inner_prod =
<B>inner_product</B>(l.begin(), l.end(), v.begin(), 0);</PRE>
<PRE> //Calculate a wacky inner product using the same values
int wacky =
<B>inner_product</B>(l.begin(), l.end(), v.begin(), 0,
plus<int>(), minus<int>());</PRE>
<PRE> //Print the output
cout << "For the two sets of numbers: " << endl
<< " ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
cout << endl << " and ";
copy(l.begin(),l.end(),ostream_iterator<int>(cout," "));</PRE>
<PRE> cout << "," << endl << endl;
cout << "The inner product is: " << inner_prod << endl;
cout << "The wacky result is: " << wacky << endl;</PRE>
<PRE> return 0;
}</PRE>
<PRE>Output :
For the two sets of numbers:
-2 -3 -2
and 6 -3 -2 ,
The inner product is: 1
The wacky result is: 8
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters, then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you'll have to write :</P>
<P><SAMP>list<int, allocator> </SAMP>and <SAMP>vector<int, allocator></SAMP></P>
<P>instead of </P>
<P><SAMP>list<int> </SAMP>and<SAMP> vector<int></SAMP></P>
<HR>
<A HREF="inc_3676.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="inp_3138.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -