/
Ikocs
/
cpp-tasks
Обзор
Документация
Войти
/
Ikocs
/
cpp-tasks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
54_KeyValueStorage/main.cpp
49 строк
1 KB
ikocs
Add 54
15 окт 2024, 11:33
15 окт 2024, 11:33
1b40493
Код
Авторство
О чём код?
#include <iostream> #include <algorithm> #include <unordered_map> template <typename Key, typename Value> class KeyValueStorage { private: std::unordered_map<Key, Value> data; public: void Insert(const Key& key, const Value& value) { data[key] = value; } void Remove(const Key& key) { data.erase(key); } bool Find(const Key& key, Value* const value = nullptr) const; }; template <typename Key, typename Value> bool KeyValueStorage<Key, Value>::Find(const Key& key, Value* const value) const { auto it = data.find(key); if (it == data.end()) return false; if (value != nullptr) { *value = it->second; } return true; } #include <string> int main() { KeyValueStorage<std::string, int> kv; kv.Insert("hello", 42); kv.Insert("bye", -13); int value = 123; auto res = kv.Find("wrong", &value); // должно вернуться false, а value не должен меняться res = kv.Find("bye", &value); // должно вернуться true, в value должно быть -13 res = kv.Find("hello", nullptr); // должно вернуться true return 0; }