#include #include #include // save lines with count struct HistEntry { std::string content; int counter; }; // global hist vector std::vector histogramm; // iterate over hist and increment counter or add new Entry void handle_bytes(std::string cont) { for(std::vector::iterator it = histogramm.begin(); it != histogramm.end(); it++) { // compare memory if (it->content == cont) { it->counter++; return; } } // nothing found, so add new entry HistEntry tmp; tmp.content = cont; tmp.counter = 1; histogramm.push_back(tmp); } void print_histogramm() { for(std::vector::iterator it = histogramm.begin(); it != histogramm.end(); it++) { std::cout << it->counter << "\t" << it->content << std::endl; } } void print_usage(std::string prog_name) { std::cout << "usage: " << prog_name << " " << std::endl; std::cout << "if is \"-\" read from stdin" << std::endl; } int main(int argc, char **argv) { if(argc != 2) { print_usage(std::string(argv[0])); return -1; } // select input stream based on command line arg bool input_stdin = false; std::string file(argv[1]); std::ifstream fs; if (file == "-") input_stdin = true; else fs.open(file, std::ios_base::in); std::istream &read_from = input_stdin ? std::cin : fs; // handle input for(std::string line; std::getline(read_from, line);) { handle_bytes(line); } print_histogramm(); return 0; }