Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Created December 18, 2014 13:59
Show Gist options
  • Save cengiz-io/9407ffefbeb8fd514d70 to your computer and use it in GitHub Desktop.
Save cengiz-io/9407ffefbeb8fd514d70 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <fstream>
#include <queue>
using namespace std;
struct OrderByLength {
bool operator() (string const &a, string const &b) {
return a.length() < b.length();
}
};
int main(int argc, char *argv[]) {
if (argc < 2) return -1;
ifstream datafile(argv[1]);
priority_queue<string, vector<string>, OrderByLength> lines;
string line;
int count = 0;
while (getline(datafile, line)) {
if (line.length() < 1)
continue;
if (count == 0) {
istringstream splitter(line);
splitter >> count;
continue;
}
lines.push(line);
}
for (int x = 0; x < count; x++) {
cout << lines.top() << endl;
lines.pop();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment