首页 > 代码库 > 《STL源码剖析》---stl_numeric.h阅读笔记

《STL源码剖析》---stl_numeric.h阅读笔记

stl_numeric.h里面的都是数值算法,与数值计算有关。

G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_numeric.h 完整列表
/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/* NOTE: This is an internal header file, included by other STL headers.
 *   You should not attempt to use it directly.
 */


#ifndef __SGI_STL_INTERNAL_NUMERIC_H
#define __SGI_STL_INTERNAL_NUMERIC_H

__STL_BEGIN_NAMESPACE

//计算init和[first last)区间元素的和
// 版本一
template <class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init) {
  for ( ; first != last; ++first)
    init = init + *first; 	// 每个元素的初值累加到init上
  return init;
}

// 版本二
template <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;
}

//计算两段元素的内积,要提供初始值init。两段元素一样长,所以第二段没提供last2
// 版本一
template <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;
}

// 版本二
template <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)
	// 以外界提供的仿函数来取代第一版本中的 operator* 和 operator+。
	// op2 作用域两元素间,op1用于op2之结果与init之间。
    init = binary_op1(init, binary_op2(*first1, *first2));
  return init;
}

//__partial_sum定义在partial_sum前面,后者调用前者
template <class InputIterator, class OutputIterator, class T>
OutputIterator __partial_sum(InputIterator first, InputIterator last,
                                   OutputIterator result, T*) {
  T value = http://www.mamicode.com/*first;>