Skip to content

Instantly share code, notes, and snippets.

@splitline
Created March 13, 2017 15:01
Show Gist options
  • Select an option

  • Save splitline/f5c0d38c838d0cfed061fec5204b0bb1 to your computer and use it in GitHub Desktop.

Select an option

Save splitline/f5c0d38c838d0cfed061fec5204b0bb1 to your computer and use it in GitHub Desktop.
// Name: 黃志仁
// Date: 2017/03/11
// Last Update: 2017/03/11
// Problem statement: Sort and output the input number with occurences
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main() {
int N;
map < int, int, greater < int > > intArr; //define a map which is sort from max to min
ifstream inFile("intArray.txt");
ofstream outFile("output.txt"); //read file
while (inFile >> N) {
if (intArr.find(N) == intArr.end()) intArr[N] = 1;
else intArr[N]++;
}
//if N exist , + counter
//else insert an element
map < int, int > ::iterator it;
outFile << "N" << "\t" << "Count" << endl;
for (it = intArr.begin(); it != intArr.end(); it++)
outFile << it -> first << "\t" << it -> second << endl; //just output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment