C++/STL Algorithms Helper/inner product

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Compute the sample standard deviation

<source lang="cpp">

  1. include <algorithm>
  2. include <cmath>
  3. include <functional>
  4. include <iostream>
  5. include <list>
  6. include <numeric>
  7. 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">

  1. include <iostream>
  2. include <vector>
  3. 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.
*/
  1. include <iostream>
  2. include <vector>
  3. include <deque>
  4. include <list>
  5. include <set>
  6. include <map>
  7. include <string>
  8. include <algorithm>
  9. include <iterator>
  10. include <functional>
  11. 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.
*/
  1. include <iostream>
  2. include <vector>
  3. include <deque>
  4. include <list>
  5. include <set>
  6. include <map>
  7. include <string>
  8. include <algorithm>
  9. include <iterator>
  10. include <functional>
  11. 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.
*/
  1. include <iostream>
  2. include <vector>
  3. include <deque>
  4. include <list>
  5. include <set>
  6. include <map>
  7. include <string>
  8. include <algorithm>
  9. include <iterator>
  10. include <functional>
  11. 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>