Taking the sum of values from a stream : istream iterator « STL Algorithms Iterator « C++ Tutorial






#include <iostream>
#include <iterator>
using std::cout;
using std::endl;
using std::cin;
using std::istream_iterator;

template <typename Iter> 

double mySum (Iter begin, Iter end) { 
  double sum = 0.0;
  
  for( ; begin != end ;)
    sum += *begin++;
  return sum;
} 

int main() {
  cout << "Ctrl-Z to stop" << endl;

  double av = mySum(istream_iterator<double>(cin), istream_iterator<double>());

  cout << "The sum value is " << av << endl;
return 0;
}
Ctrl-Z to stop
^Z
The sum value is 0








30.5.istream iterator
30.5.1.Taking the sum of values from a stream
30.5.2.Use istream_iterator to loop through a string
30.5.3.Use istream_iterator to loop through a string defined by char pointer
30.5.4.Read words from standard input, sort and print out without duplicates
30.5.5.Read string from keyboard and save to vector directly
30.5.6.istream_iterator reads and displays characters from cin until a period is received.
30.5.7.Advance istream_iterator