/* Lecture code 7.0
 *
 * STL Algorithms to load data from a file and average the values, among other things.
 * Demonstrates istream_iterator, back_inserter, copy, accumulate, sort, binary_search.
 */

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>  // For istream_iterator, back_inserter
#include <algorithm> // For copy, sort, binary_search
#include <numeric>   // For accumulate
using namespace std;

int main()
{
	ifstream input("numbers.txt");
	vector<int> myVector;

	/* Load the contents of the file into the vector. */
	copy(istream_iterator<int>(input), istream_iterator<int>(), back_inserter(myVector));

	/* Calculate the average value. */
	cout << accumulate(myVector.begin(), myVector.end(), 0.0) / myVector.size() << endl;
	
	/* Sort everything and do a binary search for 137. */
	sort(myVector.begin(), myVector.end());
	if(binary_search(myVector.begin(), myVector.end(), 137))
		cout << "Found 137!" << endl;
	else
		cout << "Didn't find 137." << endl
	
	return 0;
}