/* Lecture code 17.0
 *
 * Using a functor to count numbers less than some threshold, part one.
 */
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;

/* A functor class that stores a value, then lets clients query whether some
 * particular integer is less than that value.
 */
class LessThan {
public:
	/* Constructor takes in the cutoff value, then stores it. */
	LessThan(int cutoff) {
		threshold = cutoff;
	}

	/* Function call operator (operator()) takes in a value and compares it against
	 * the stored threshold.
	 */
	bool operator() (int value) const {
		return value < threshold;
	}

private:
	int threshold;
};

int main() {
	/* Read a cutoff from the user.  You should almost certainly use a GetInteger()-esque
	 * function here, but in this limited case we can squeak by with cin because we trust
	 * the user.
	 */
	cout << "Enter a cutoff: ";
	int cutoff;
	cin >> cutoff;

	/* Open the file for reading. */
	ifstream input("numbers.txt");
	
	/* Using a pair of istream_iterators as the input range (that is, iterators spanning all
	 * the file contents), count how many times a value in the range is less than the cutoff
	 * value.  Note that LessThan(cutoff) means "construct a temporary LessThan object, passing
	 * in cutoff as a constructor argument" rather than "Call LessThan's operator(), passing in
	 * cutoff."
	 */
	cout << count_if(istream_iterator<int>(input), istream_iterator<int>(), LessThan(cutoff)) << endl;
	
}