/* Chapter 11, exercise 16. Write a program to read a file of whitespace - separated numbers and 
output them in order (lowest value first), one value per line.  Write a value only once, and if 
it occurs more than once write the count of its occurrences on its line.For example, 7 5 5 7 3 117 5 
should give
3
5 3
7 2
117
*/

#include "std_lib_facilities.h"

int main()  {
	cout << "Enter input file name: ";
	string ifname;
	cin >> ifname;
	ifstream ifs(ifname.c_str());
	if (!ifs) error("can't open input file ", ifname);

	// read numbers
	int n;
	vector<int> numbers;
	while (ifs >> n)
		numbers.push_back(n);

	// sort and print
	sort(numbers.begin(), numbers.end());
	int counter = 1;
	for (int i = 0; i < numbers.size(); ++i) {
		if (i == 0 || numbers[i] != numbers[i - 1]) {
			if (counter > 1) 
				cout << "\t" << counter;
			cout << endl;
			counter = 1;
			cout << numbers[i];
		}
		else ++counter; //i != 0 and numbers[i] == numbers[i-1]
	}
}