1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <functional>
#include <cmath>
#include <map>
#include <tuple>
#include <cstdlib>
#include <cstdio>
#include <limits>
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 2)
{
cout << "Usage: ./components <components_file>" << endl;
return 0;
}
string vname, compname;
ifstream inputvert(argv[1]);
map<string, int> compcounts;
vector< pair<int, string > > countbyname;
while(inputvert >> vname)
{
inputvert >> compname;
auto it = compcounts.insert(make_pair(compname,1));
if (!it.second) // already there
{
(it.first->second)++;
}
}
cout << "distinct components " << compcounts.size() << endl;
for (auto it = compcounts.begin(); it != compcounts.end(); ++it)
{
countbyname.push_back(make_pair(it->second, it->first));
}
sort(countbyname.begin(), countbyname.end());
for (auto it = countbyname.begin(); it != countbyname.end(); ++it)
{
cout << it ->second << " " << it->first << endl;
}
}
|