/* Lecture code 17.4
 *
 * Using a functor to count numbers less than some threshold, part two.
 * This version uses the bind2nd and less utility functions from
 * <functional> to get the job done.  Look at how little code is required
 * to scan the entire file, checking for a number of values below a
 * cutoff!  This is the real beauty of the STL when all the pieces are in
 * play.
 */
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <functional> // For bind2nd, less
using namespace std;

int main() {
	cout << "Enter a cutoff: ";
	int cutoff;
	cin >> cutoff;

	ifstream input("numbers.txt");
	
	/* The call
	 *
	 *     bind2nd(less<int>(), cutoff)
	 *
	 * means "a function equivalent to less<int>(), but with the second parameter locked in place as
	 * cutoff."  Since less<int>() compares whether one integer is less than another, this function
	 * checks whether a value is less than cutoff.
	 */
	cout << count_if(istream_iterator<int>(input), istream_iterator<int>(), bind2nd(less<int>(), cutoff)) << endl;
	
}