Skip to content

Instantly share code, notes, and snippets.

@epatel
Created July 29, 2011 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epatel/1114501 to your computer and use it in GitHub Desktop.
Save epatel/1114501 to your computer and use it in GitHub Desktop.
Counting words (car names) and sorting occurrences
#include <iostream>
#include <list>
#include <map>
using namespace std;
bool my_pair_compare(pair<string,int> &a, pair<string,int> &b) {
return a.second > b.second;
}
void my_pair_output(pair<string,int> &p) {
cout << p.first << " " << p.second << endl;
}
int main() {
map<string,int> cars;
while (1) {
string name;
cin >> name;
if (cin.eof()) break;
cars[name]++;
}
list<pair<string,int> > names;
map<string,int>::iterator citer = cars.begin();
while (citer != cars.end())
names.push_back(*citer++);
names.sort(my_pair_compare);
for_each(names.begin(), names.end(), my_pair_output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment