prog20_08.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 23 行

CPP
23
字号
// Program 20.8 Averaging values from Integer  File: prog20_08.cpp
#include <iostream>
#include "Integer.h"
using std::cout;
using std::endl;

template <typename Iter> 
double average(Iter a, Iter end) { 
  double sum = 0.0;
  int count = 0;  
  for( ; a != end ; ++count)
    sum += *a++;
  return sum/count;             // Lets bad things happen when count==0
} 

int main() {
  Integer first(1);
  Integer last(11);
  cout << "The average of the integers from " << *first << " to " << *last-1;
  cout << " is " << average(first, last) << endl;
  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?