From: Richard Kojedzinszky Date: Tue, 14 Apr 2015 15:41:47 +0000 (+0200) Subject: uwc: not using std::string* pointers X-Git-Url: http://git.neszt.hu/?a=commitdiff_plain;h=154bd7e9bffb1e69766851cc8f5f097b2fd8059d;p=uwc.git uwc: not using std::string* pointers --- diff --git a/uwc.cpp b/uwc.cpp index 1786b3f..09d197e 100644 --- a/uwc.cpp +++ b/uwc.cpp @@ -2,10 +2,10 @@ #include #include #include +#include typedef std::unordered_map hash_t; - int main() { std::cin.sync_with_stdio(false); @@ -23,20 +23,18 @@ int main() } } - std::vector words(hash.size()); - int i = 0; + std::vector words; for (const auto& e : hash) { - words[i++] = &(e.first); - // words.push_back(&(e.first)); + words.push_back(e.first); } - std::sort(words.begin(), words.end(), [&hash] (const std::string* a, const std::string* b) -> bool { - int diff = hash[*a] - hash[*b]; - return (diff < 0) || (diff == 0 && (*a) < (*b)); // (a.compare(b) < 0); + std::sort(words.begin(), words.end(), [&hash] (const std::string& a, const std::string& b) -> bool { + int diff = hash[a] - hash[b]; + return (diff < 0) || (diff == 0 && a < b); // (a.compare(b) < 0); }); - for (const auto e : words) { - std::cout << *e << " " << hash[*e] << "\n"; + for (const auto& e : words) { + std::cout << e << " " << hash[e] << "\n"; } }