#include #include #include #include // TODO: implement a dictionary that maps keys to (multiple) values // start with multimap.cpp, copy most of the code, and modify it class Translations { public: Translations(std::string&& value) { // TODO: construct the set with the given value } void add(std::string&& value) { // TODO: add the value to the multimap } bool remove(const std::string& value) { return {}; // TODO: return true if the `values` set is empty after removing the value } void list(std::ostream& out) const { } void list(std::string_view name, std::ostream& out) const { } private: std::unordered_set values; }; class Dictionary { public: void add(std::string&& key, std::string&& value) { // TODO: add the value to the multimap // hint: use translations.try_emplace for adding new keys (without finding the key twice) } void remove(const std::string& key) { // TODO: remove all values associated with the key } void remove(const std::string& key, const std::string& value) { // TODO: remove the value associated with the key // and remove the `Translations` object if it is empty (use the remove method of `Translations`) } // NOTE: this method is const, because it does not modify the dictionary void find(const std::string& key, std::ostream& out) const { // TODO: print all values associated with the key } // NOTE: this method is const, because it does not modify the dictionary void list(std::ostream& out) const { // TODO: print all key-value pairs in the dictionary as "key: value" } private: std::unordered_map translations; }; void program_loop(Dictionary& dictionary, std::istream& in, std::ostream& out) { std::string line; std::string command; // TODO: read commands from stdin: // - add : add a new key-value pair to the dictionary // - remove : remove a specific key-value pair from the dictionary // - remove : remove all key-value pairs with the given key // - find : print all values associated with the given key // - list: print all key-value pairs in the dictionary // - quit: exit the program while (std::getline(in, line)) { std::istringstream iss(std::move(line)); if (!(iss >> command)) { continue; } if (command == "add") { } else if (command == "remove") { } else if (command == "find") { } else if (command == "list") { } else if (command == "quit") { break; } else { out << "Unknown command: " << command << std::endl; } } } int main() { Dictionary dictionary; program_loop(dictionary, std::cin, std::cout); }