#include #include #include #include #include #include "editdistance.h" struct record_t { std::size_t edit_distance; std::ptrdiff_t count; std::string word; friend bool operator<(const record_t& lhs, const record_t& rhs) noexcept { return std::forward_as_tuple(lhs.edit_distance, -lhs.count, lhs.word) < std::forward_as_tuple(rhs.edit_distance, -rhs.count, rhs.word); } }; int main(int argc, char** argv) { auto to_lower = [](std::string& s) { std::transform(s.begin(), s.end(), s.begin(), [](char c) { return std::tolower(c); }); }; std::string search; if (argc == 2) { search = argv[1]; to_lower(search); } else { std::cerr << "Must provide search term.\n"; return 1; } std::vector records; { std::string word; std::ptrdiff_t count; while (std::cin >> kblib::get_line(word, '\t') >> count >> kblib::nl) { to_lower(word); records.push_back({edit_distance(search, word), count, word}); } } std::sort(records.begin(), records.end()); for (auto rec : records) { std::cout << rec.word << '\t' << rec.edit_distance << '\t' << rec.count << '\n'; } return 0; }