Skip to content

Instantly share code, notes, and snippets.

@pce
Last active August 13, 2020 22:29
Show Gist options
  • Save pce/8008e987a85215717b4a5332bc1eb9bc to your computer and use it in GitHub Desktop.
Save pce/8008e987a85215717b4a5332bc1eb9bc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using std::vector;
using std::cout;
using std::unordered_map;
using std::string;
// check if the key is present in the hash-table or not
bool isset_key(unordered_map<string, vector<string>> dict, string key)
{
if (dict.find(key) == dict.end())
return false;
return true;
}
int main() {
// unordered_map <string, vector<string>> dict;
unordered_map<string, vector<string>> dict = {
{"本",{"ほん (hon)", "もと (moto)" }}
};
// Add another entry to the map
dict["日"] = vector<string> {"にち (nichi)", "ひ (hi)"};
string key = "本";
cout << "key: " << key << "\n";
auto definitions = dict[key];
for (const string& definition : definitions) {
cout << definition << "\n";
}
key = "四";
if (!isset_key(dict, key)) {
cout << "The key '四 (four)' is not in the dictionary." << "\n";
}
/* > g++ -std=c++11 unorderd_map.cpp&& ./a.out
key: 本
ほん (hon)
もと (moto)
The key '四 (four)' is not in the dictionary.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment