Standard C++ Library Class Reference
int inner_prod =
inner_product(l.begin(), l.end(), v.begin(), 0);
//Calculate a wacky inner product using the same values
int wacky =
inner_product(l.begin(), l.end(), v.begin(), 0,
plus<int>(), minus<int>());
//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," "));
cout << "," << endl << endl;
cout << "The inner product is: " << inner_prod << endl;
cout << "The wacky result is: " << wacky << endl;
return 0;
}
Output :
For the two sets of numbers:
-2 -3 -2
and 6 -3 -2 ,
The inner product is: 1
The wacky result is: 8
Warnings
If your compiler does not support default template parameters, then you need to always supply
the Allocator template argument. For instance, you'll have to write :
list<int, allocator> and vector<int, allocator>
instead of
list<int> and vector<int>