|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <unordered_map> |
| 4 | + |
| 5 | +////////////////////////////////////////////////// |
| 6 | +// reference: http://en.cppreference.com/w/cpp/container/unordered_map |
| 7 | +int test_unordered_map1() |
| 8 | +{ |
| 9 | + // Create an unordered_map of three strings (that map to strings) |
| 10 | + std::unordered_map<std::string, std::string> u = { |
| 11 | + { "RED", "#FF0000" }, |
| 12 | + { "GREEN", "#00FF00" }, |
| 13 | + { "BLUE", "#0000FF" } |
| 14 | + }; |
| 15 | + |
| 16 | + // Iterate and print keys and values of unordered_map |
| 17 | + for (const auto& n : u) { |
| 18 | + std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n"; |
| 19 | + } |
| 20 | + |
| 21 | + // Add two new entries to the unordered_map |
| 22 | + u["BLACK"] = "#000000"; |
| 23 | + u["WHITE"] = "#FFFFFF"; |
| 24 | + |
| 25 | + // Output values by key |
| 26 | + std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n"; |
| 27 | + std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n"; |
| 28 | + |
| 29 | + std::cout << "The u's size: " << u.size() << std::endl; |
| 30 | + |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +///////////////////////////////////////////////////// |
| 35 | +// reference: http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ |
| 36 | +typedef std::unordered_map<std::string, std::string> stringmap; |
| 37 | + |
| 38 | +stringmap merge(stringmap a, stringmap b) { |
| 39 | + stringmap temp(a); temp.insert(b.begin(), b.end()); return temp; |
| 40 | +} |
| 41 | + |
| 42 | +int test_unordered_map2() |
| 43 | +{ |
| 44 | + ////////// at/size |
| 45 | + std::unordered_map<std::string, int> mymap = { { "Mars", 3000 }, { "Saturn", 60000 }, { "Jupiter", 70000 } }; |
| 46 | + |
| 47 | + mymap.at("Mars") = 3396; |
| 48 | + mymap.at("Saturn") += 272; |
| 49 | + mymap.at("Jupiter") = mymap.at("Saturn") + 9638; |
| 50 | + |
| 51 | + for (auto& x : mymap) { |
| 52 | + std::cout << x.first << ": " << x.second << std::endl; |
| 53 | + } |
| 54 | + |
| 55 | + std::cout << "mymap.size() is " << mymap.size() << std::endl; |
| 56 | + |
| 57 | + /////////// begin |
| 58 | + std::unordered_map<std::string, std::string> mymap2 = { { "Australia", "Canberra" }, { "U.S.", "Washington" }, { "France", "Paris" } }; |
| 59 | + |
| 60 | + std::cout << "mymap2 contains:"; |
| 61 | + for (auto it = mymap2.begin(); it != mymap2.end(); ++it) |
| 62 | + std::cout << " " << it->first << ":" << it->second; |
| 63 | + std::cout << std::endl; |
| 64 | + |
| 65 | + std::cout << "mymap2's buckets contain:\n"; |
| 66 | + for (unsigned i = 0; i < mymap2.bucket_count(); ++i) { |
| 67 | + std::cout << "bucket #" << i << " contains:"; |
| 68 | + for (auto local_it = mymap2.begin(i); local_it != mymap2.end(i); ++local_it) |
| 69 | + std::cout << " " << local_it->first << ":" << local_it->second; |
| 70 | + std::cout << std::endl; |
| 71 | + } |
| 72 | + |
| 73 | + ////////////// bucket |
| 74 | + std::unordered_map<std::string, std::string> mymap3 = { |
| 75 | + { "us", "United States" }, |
| 76 | + { "uk", "United Kingdom" }, |
| 77 | + { "fr", "France" }, |
| 78 | + { "de", "Germany" } |
| 79 | + }; |
| 80 | + |
| 81 | + for (auto& x : mymap3) { |
| 82 | + std::cout << "Element [" << x.first << ":" << x.second << "]"; |
| 83 | + std::cout << " is in bucket #" << mymap3.bucket(x.first) << std::endl; |
| 84 | + } |
| 85 | + |
| 86 | + /////////////// count |
| 87 | + std::unordered_map<std::string, double> mymap4 = { |
| 88 | + { "Burger", 2.99 }, |
| 89 | + { "Fries", 1.99 }, |
| 90 | + { "Soda", 1.50 } }; |
| 91 | + |
| 92 | + for (auto& x : { "Burger", "Pizza", "Salad", "Soda" }) { |
| 93 | + if (mymap4.count(x)>0) |
| 94 | + std::cout << "mymap4 has " << x << std::endl; |
| 95 | + else |
| 96 | + std::cout << "mymap4 has no " << x << std::endl; |
| 97 | + } |
| 98 | + |
| 99 | + ///////////////// erase |
| 100 | + std::unordered_map<std::string, std::string> mymap5; |
| 101 | + |
| 102 | + // populating container: |
| 103 | + mymap5["U.S."] = "Washington"; |
| 104 | + mymap5["U.K."] = "London"; |
| 105 | + mymap5["France"] = "Paris"; |
| 106 | + mymap5["Russia"] = "Moscow"; |
| 107 | + mymap5["China"] = "Beijing"; |
| 108 | + mymap5["Germany"] = "Berlin"; |
| 109 | + mymap5["Japan"] = "Tokyo"; |
| 110 | + |
| 111 | + // erase examples: |
| 112 | + mymap5.erase(mymap5.begin()); // erasing by iterator |
| 113 | + mymap5.erase("France"); // erasing by key |
| 114 | + mymap5.erase(mymap5.find("China"), mymap5.end()); // erasing by range |
| 115 | + |
| 116 | + // show content: |
| 117 | + for (auto& x : mymap5) |
| 118 | + std::cout << x.first << ": " << x.second << std::endl; |
| 119 | + |
| 120 | + ////////////////////// find |
| 121 | + std::unordered_map<std::string, double> mymap6 = { |
| 122 | + { "mom", 5.4 }, |
| 123 | + { "dad", 6.1 }, |
| 124 | + { "bro", 5.9 } }; |
| 125 | + |
| 126 | + std::string input; |
| 127 | + std::cout << "who? "; |
| 128 | + getline(std::cin, input); |
| 129 | + |
| 130 | + std::unordered_map<std::string, double>::const_iterator got = mymap6.find(input); |
| 131 | + |
| 132 | + if (got == mymap6.end()) |
| 133 | + std::cout << "not found"; |
| 134 | + else |
| 135 | + std::cout << got->first << " is " << got->second; |
| 136 | + |
| 137 | + std::cout << std::endl; |
| 138 | + |
| 139 | + //////////////////// insert |
| 140 | + std::unordered_map<std::string, double> |
| 141 | + myrecipe, |
| 142 | + mypantry = { { "milk", 2.0 }, { "flour", 1.5 } }; |
| 143 | + |
| 144 | + std::pair<std::string, double> myshopping("baking powder", 0.3); |
| 145 | + |
| 146 | + myrecipe.insert(myshopping); // copy insertion |
| 147 | + myrecipe.insert(std::make_pair<std::string, double>("eggs", 6.0)); // move insertion |
| 148 | + myrecipe.insert(mypantry.begin(), mypantry.end()); // range insertion |
| 149 | + myrecipe.insert({ { "sugar", 0.8 }, { "salt", 0.1 } }); // initializer list insertion |
| 150 | + |
| 151 | + std::cout << "myrecipe contains:" << std::endl; |
| 152 | + for (auto& x : myrecipe) |
| 153 | + std::cout << x.first << ": " << x.second << std::endl; |
| 154 | + |
| 155 | + std::cout << std::endl; |
| 156 | + |
| 157 | + //////////////////// = |
| 158 | + stringmap first = { { "AAPL", "Apple" }, { "MSFT", "Microsoft" } }; // init list |
| 159 | + stringmap second = { { "GOOG", "Google" }, { "ORCL", "Oracle" } }; // init list |
| 160 | + stringmap third = merge(first, second); // move |
| 161 | + first = third; // copy |
| 162 | + |
| 163 | + std::cout << "first contains:"; |
| 164 | + for (auto& elem : first) std::cout << " " << elem.first << ":" << elem.second; |
| 165 | + std::cout << std::endl; |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +////////////////////////////////////////////////////// |
| 171 | +// reference: http://www.geeksforgeeks.org/unordered_map-in-stl-and-its-applications/ |
| 172 | +int test_unordered_map3() |
| 173 | +{ |
| 174 | + // key will be of string type and mapped value will be of double type |
| 175 | + std::unordered_map<std::string, double> umap; |
| 176 | + |
| 177 | + // inserting values by using [] operator |
| 178 | + umap["PI"] = 3.14; |
| 179 | + umap["root2"] = 1.414; |
| 180 | + umap["root3"] = 1.732; |
| 181 | + umap["log10"] = 2.302; |
| 182 | + umap["loge"] = 1.0; |
| 183 | + |
| 184 | + // inserting value by insert function |
| 185 | + umap.insert(std::make_pair("e", 2.718)); |
| 186 | + |
| 187 | + std::string key = "PI"; |
| 188 | + |
| 189 | + // If key not found in map iterator to end is returned |
| 190 | + if (umap.find(key) == umap.end()) { |
| 191 | + std::cout << key << " not found\n\n"; |
| 192 | + } else {// If key found then iterator to that key is returned |
| 193 | + std::cout << "Found " << key << "\n\n"; |
| 194 | + } |
| 195 | + |
| 196 | + key = "lambda"; |
| 197 | + if (umap.find(key) == umap.end()) |
| 198 | + std::cout << key << " not found\n"; |
| 199 | + else |
| 200 | + std::cout << "Found " << key << std::endl; |
| 201 | + |
| 202 | + // iterating over all value of umap |
| 203 | + std::unordered_map<std::string, double>::iterator itr; |
| 204 | + std::cout << "\nAll Elements : \n"; |
| 205 | + for (itr = umap.begin(); itr != umap.end(); itr++) { |
| 206 | + // itr works as a pointer to pair<string, double> type itr->first stores the key part |
| 207 | + // and itr->second stroes the value part |
| 208 | + std::cout << itr->first << " " << itr->second << std::endl; |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +int main(){ |
| 217 | + test_unordered_map1(); |
| 218 | + test_unordered_map2(); |
| 219 | + test_unordered_map3(); |
| 220 | + return 0; |
| 221 | +} |
0 commit comments