/* Lecture code 8.0 * * STL Algorithms to load data from a file and average the values. * Demonstrates istream_iterator and accumulate, as well as exactly * how concise the STL can be! * * This code requires a data file numbers.txt to be in the same directory * as the program. */ #include #include #include #include #include // For istream_iterator #include // For accumulate using namespace std; int main() { ifstream input("numbers.txt"); vector myVector; /* Load the contents of the file into the vector. */ myVector.insert(myVector.begin(), istream_iterator(input), istream_iterator()); /* Calculate the average value. */ cout << accumulate(myVector.begin(), myVector.end(), 0.0) / myVector.size() << endl; return 0; }