uwc: not using std::string* pointers master
authorRichard Kojedzinszky <krichy@tvnetwork.hu>
Tue, 14 Apr 2015 15:41:47 +0000 (17:41 +0200)
committerRichard Kojedzinszky <krichy@tvnetwork.hu>
Tue, 14 Apr 2015 15:43:27 +0000 (17:43 +0200)
uwc.cpp

diff --git a/uwc.cpp b/uwc.cpp
index 1786b3f..09d197e 100644 (file)
--- a/uwc.cpp
+++ b/uwc.cpp
@@ -2,10 +2,10 @@
 #include <unordered_map>
 #include <string>
 #include <iostream>
+#include <typeinfo>
 
 typedef std::unordered_map<std::string, int> hash_t;
 
-
 int main()
 {
        std::cin.sync_with_stdio(false);
@@ -23,20 +23,18 @@ int main()
                }
        }
 
-       std::vector<const std::string*> words(hash.size());
-       int i = 0;
+       std::vector<std::string> 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";
        }
 }