Use Iter with array : template iterators « STL Algorithms Iterator « C++






Use Iter with array

 
 

#include <iostream>
#include <vector> 
using std::cout;
using std::endl;
using std::vector;

template <typename Iter> 
double vectorSum(Iter begin, Iter end) {
  double sum = 0.0;
  
  while( begin != end )
    sum += *begin++;
  return sum;        
} 

int main() {
  double temperature[] = { 10.5, 20.0, 8.5 }; 
  cout << "array vectorSum = " 
       << vectorSum(temperature,temperature+sizeof temperature/sizeof temperatu
re[0]) 
       << endl;

  return 0;
}

/* 
array vectorSum = 39

 */        
  








Related examples in the same category

1.Computing the sum with template iterators