STL source code analysis stl_numeric.h

Source: Internet
Author: User

This article is senlie original, reproduced Please retain this address: http://blog.csdn.net/zhengsenlie


Description, source code, and example
Version 1: normal operation version
Version 2: General Operation version


1. accumulate
Description: calculates the sum of all elements in init and [first, last ).
Source code:

//version 1template <class InputIterator, class T>T accumulate(InputIterator first, InputIterator last, T init) {  for ( ; first != last; ++first)    init = init + *first;  return init;}//version 2template <class InputIterator, class T, class BinaryOperation>T accumulate(InputIterator first, InputIterator last, T init,             BinaryOperation binary_op) {  for ( ; first != last; ++first)    init = binary_op(init, *first);  return init;}

Example:
int main(){  int A[] = {1, 2, 3, 4, 5};  const int N = sizeof(A) / sizeof(int);  cout << "The sum of all elements in A is "        << accumulate(A, A + N, 0) // 15       << endl;  cout << "The product of all elements in A is "       << accumulate(A, A + N, 1, multiplies<int>()) //120       << endl;}

2. inner_product
Description: calculates the Inner Product of [first1, last1) and [first2, first2 + (last1-first1 ).
Source code:
//version 1template <class InputIterator1, class InputIterator2, class T>T inner_product(InputIterator1 first1, InputIterator1 last1,                InputIterator2 first2, T init) {  for ( ; first1 != last1; ++first1, ++first2)    init = init + (*first1 * *first2);  return init;}//version 2template <class InputIterator1, class InputIterator2, class T,          class BinaryOperation1, class BinaryOperation2>T inner_product(InputIterator1 first1, InputIterator1 last1,                InputIterator2 first2, T init, BinaryOperation1 binary_op1,                BinaryOperation2 binary_op2) {  for ( ; first1 != last1; ++first1, ++first2)    init = binary_op1(init, binary_op2(*first1, *first2));  return init;}

Example:
int main(){  int A1[] = {1, 2, 3};  int A2[] = {4, 1, -2};  const int N1 = sizeof(A1) / sizeof(int);  cout << "The inner product of A1 and A2 is "        << inner_product(A1, A1 + N1, A2, 0) //0       << endl;}

3. partial_sum
Description: calculates the local sum.
Source code:
//version 1template <class InputIterator, class OutputIterator, class T>OutputIterator __partial_sum(InputIterator first, InputIterator last,                             OutputIterator result, T*) {  T value = *first;  while (++first != last) {    value = value + *first;    *++result = value;  }  return ++result;}template <class InputIterator, class OutputIterator>OutputIterator partial_sum(InputIterator first, InputIterator last,                           OutputIterator result) {  if (first == last) return result;  *result = *first;  return __partial_sum(first, last, result, value_type(first));}//version 2template <class InputIterator, class OutputIterator, class T,          class BinaryOperation>OutputIterator __partial_sum(InputIterator first, InputIterator last,                             OutputIterator result, T*,                             BinaryOperation binary_op) {  T value = *first;  while (++first != last) {    value = binary_op(value, *first);    *++result = value;  }  return ++result;}template <class InputIterator, class OutputIterator, class BinaryOperation>OutputIterator partial_sum(InputIterator first, InputIterator last,                           OutputIterator result, BinaryOperation binary_op) {  if (first == last) return result;  *result = *first;  return __partial_sum(first, last, result, value_type(first), binary_op);}

Example:
int main(){  const int N = 10;  int A[N];  fill(A, A+N, 1);  cout << "A:                 ";  copy(A, A+N, ostream_iterator<int>(cout, " ")); // 1 2 3 4 5 6 7 8 9 10  cout << endl;   cout << "Partial sums of A: ";  partial_sum(A, A+N, ostream_iterator<int>(cout, " ")); // 1 3 6 10 15 21 28 36 45 55  cout << endl;}  

4. adjacent_difference
Description: calculates the difference between the adjacent elements of [first, last ).
Source code:
//version 1template <class InputIterator, class OutputIterator, class T>OutputIterator __adjacent_difference(InputIterator first, InputIterator last,                                      OutputIterator result, T*) {  T value = *first;  while (++first != last) {    T tmp = *first;    *++result = tmp - value;    value = tmp;  }  return ++result;}template <class InputIterator, class OutputIterator>OutputIterator adjacent_difference(InputIterator first, InputIterator last,                                    OutputIterator result) {  if (first == last) return result;  *result = *first;  return __adjacent_difference(first, last, result, value_type(first));}//version 2template <class InputIterator, class OutputIterator, class T,           class BinaryOperation>OutputIterator __adjacent_difference(InputIterator first, InputIterator last,                                      OutputIterator result, T*,                                     BinaryOperation binary_op) {  T value = *first;  while (++first != last) {    T tmp = *first;    *++result = binary_op(tmp, value);    value = tmp;  }  return ++result;}template <class InputIterator, class OutputIterator, class BinaryOperation>OutputIterator adjacent_difference(InputIterator first, InputIterator last,                                   OutputIterator result,                                   BinaryOperation binary_op) {  if (first == last) return result;  *result = *first;  return __adjacent_difference(first, last, result, value_type(first),                               binary_op);}

Example:
int main(){  int A[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};  const int N = sizeof(A) / sizeof(int);  int B[N];  cout << "A[]:         ";  copy(A, A + N, ostream_iterator<int>(cout, " ")); //1 4 9 16 25 36 49 64 81 100  cout << endl;    adjacent_difference(A, A + N, B);  cout << "Differences: ";  copy(B, B + N, ostream_iterator<int>(cout, " ")); // 3 5 7 9 11 13 15 17 19  cout << endl;  cout << "Reconstruct: ";  partial_sum(B, B + N, ostream_iterator<int>(cout, " ")); //1 4 9 16 25 36 49 64 81 100  cout << endl;}

5. Power
Description: calculates the N power of a certain number. SGI exclusive, not in STL Standard
Source code:
// Returns x ** n, where n> = 0. note that "multiplication" // is required to be associative, but not necessarily commutative. template <class T, class integer, class monoidoperation> T power (t x, integer N, monoidoperation OP) {// here Russian peasant algorithm if (n = 0) return identity_element (OP); else {While (N & 1) = 0) {n >>= 1; X = OP (x, x );} t result = x; n >>= 1; while (n! = 0) {x = OP (x, x); If (N & 1 )! = 0) Result = OP (result, x); N >>= 1 ;}return result ;}template <class T, class integer> inline t power (t x, integer N) {return power (x, N, multiplies <t> ());}

Example:
Int main () {cout <"2 ** 30 =" <power (2, 30) <Endl; // --> I cannot compile. The identifier power cannot be found. I have included <numeric>}

6. iota
Description: sets the content of a certain interval so that each element in the interval starts from the specified value and is displayed in ascending state.
Source code:
template <class ForwardIterator, class T>void iota(ForwardIterator first, ForwardIterator last, T value) {  while (first != last) *first++ = value++;}

Example:
int main(){  vector<int> V(10);  iota(V.begin(), V.end(), 7);  copy(V.begin(), V.end(), ostream_iterator<int>(cout, " ")); // 7 8 9 10 11 12 13 14 15 16  cout << endl; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.