C++/STL Algorithms Helper/inner product
Содержание
- 1 Compute the sample standard deviation
- 2 inner_product( ) produces a summation of the product of corresponding elements in two sequences and returns the result.
- 3 Use inner_product to calculate inner reverse product
- 4 Use inner_product to process sum of all products(0 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6)
- 5 Use inner_product with inner and outer operation
Compute the sample standard deviation
<source lang="cpp">
- include <algorithm>
- include <cmath>
- include <functional>
- include <iostream>
- include <list>
- include <numeric>
- include <vector>
using namespace std; template <class T> void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){ std::cout << *i << endl; }
} int main( ) {
const float a[] = { 1, 1.3, 1.5}; vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) ); print( data ); float mean = accumulate( data.begin(), data.end(), 0.0f )/ data.size(); cout << mean; vector<float> zero_mean( data ); transform( zero_mean.begin(), zero_mean.end(), zero_mean.begin(),bind2nd( minus<float>(), mean ) ); float deviation = inner_product( zero_mean.begin(),zero_mean.end(), zero_mean.begin(), 0.0f ); deviation = sqrt( deviation / ( data.size() - 1 ) ); cout << deviation;
}
</source>
inner_product( ) produces a summation of the product of corresponding elements in two sequences and returns the result.
<source lang="cpp">
- include <iostream>
- include <vector>
- include <numeric>
using namespace std;
int main() {
vector<int> v1(5), v2(5); int i, total; for(i=0; i<5; i++) v1[i] = i; for(i=0; i<5; i++) v2[i] = i+2; total = inner_product(v1.begin(), v1.end(),v2.begin(), 0); cout << total; return 0;
}
</source>
Use inner_product to calculate inner reverse product
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
list<int> coll; INSERT_ELEMENTS(coll,1,6); PRINT_ELEMENTS(coll); /* process sum of 1*6 ... 6*1 * (0 + 1*6 + 2*5 + 3*4 + 4*3 + 5*2 + 6*1) */ cout << "inner reverse product: " << inner_product (coll.begin(), coll.end(), // first range coll.rbegin(), // second range 0) // initial value << endl;
} /* 1 2 3 4 5 6 inner reverse product: 56
*/ </source>
Use inner_product to process sum of all products(0 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6)
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
list<int> coll; INSERT_ELEMENTS(coll,1,6); PRINT_ELEMENTS(coll); /* process sum of all products * (0 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 + 6*6) */ cout << "inner product: " << inner_product (coll.begin(), coll.end(), // first range coll.begin(), // second range 0) // initial value << endl;
} /* 1 2 3 4 5 6 inner product: 91
*/ </source>
Use inner_product with inner and outer operation
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <vector>
- include <deque>
- include <list>
- include <set>
- include <map>
- include <string>
- include <algorithm>
- include <iterator>
- include <functional>
- include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */
template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {
typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << " "; } std::cout << std::endl;
} /* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection * - NOTE: NO half-open range */
template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) {
for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); }
} using namespace std; int main() {
list<int> coll; INSERT_ELEMENTS(coll,1,6); PRINT_ELEMENTS(coll); /* process product of all sums * (1 * 1+1 * 2+2 * 3+3 * 4+4 * 5+5 * 6+6) */ cout << "product of sums: " << inner_product (coll.begin(), coll.end(), // first range coll.begin(), // second range 1, // initial value multiplies<int>(), // inner operation plus<int>()) // outer operation << endl;
} /* 1 2 3 4 5 6 product of sums: 46080
*/ </source>