#include #include #include #include #include #include int hamming_distance(std::string a, std::string b) { if (a.size() > b.size()) std::swap(a, b); int distance { std::inner_product(std::begin(a), std::end(a), std::begin(b), 0, std::plus{}, [](char a, char b) { return a != b; }) }; return distance + (b.size() - a.size()); } int main() { std::ifstream ifs { "wordlist.txt" }; std::vector wordlist { std::istream_iterator{ ifs }, std::istream_iterator{ } }; std::cout << "Enter your word: "; std::string word { }; std::cin >> word; auto it = std::next(std::begin(wordlist), 5); std::partial_sort(std::begin(wordlist), std::end(wordlist), it, [&word](std::string const& a, std::string const& b) { int hamming_a { hamming_distance(a, word) }; int hamming_b { hamming_distance(b, word) }; return hamming_a < hamming_b; }); std::cout << "The five closest words are: " << std::endl; std::copy(std::begin(wordlist), it, std::ostream_iterator{ std::cout, "\n" }); }