Tuesday, December 19, 2017

C++98: std::inner_product()

The std::inner_product() template function computes the inner product of two sequences. Here is an example:

#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
  // Alternatively, 
  //   Properties > Configuration Properties > C/C++ > Code Generation >
  //                Security Check > Disable Security Check(/GS-)

#include 
#include 

int main()
{
  int vector1[3]   = {1, 2, 3};
  int vector2[3]   = {4, 5, 6};
  int innerProduct = 0;

  innerProduct = std::inner_product(&vector1[0],&vector1[3],&vector2[0],0);

  std::cout << innerProduct << std::endl; // 32 = 1*4 + 2*5 + 3*6
  
  return 0;
}
// Output: 32
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 427-8.

No comments:

Post a Comment