#include <iostream>
#include <string>
#include <map>
class ValueHolder
{
public:
int **put(const std::string &key)
{
//throw if key exists
auto val = values.emplace(key, nullptr);
return &val.first->second;
}
void init()
{
for (auto &it: values)
it.second = new int(10);
}
void print()
{
for (auto &it: values)
std::cout<<*it.second<<std::endl;
}
private:
std::map<std::string, int *> values;
};
int main()
{
ValueHolder holder;
int **ptr = holder.put("Test");
std::cout<<"ptr == nullptr: "<< (*ptr == nullptr)<<std::endl;
holder.init();
std::cout<<"ptr == nullptr: "<< (*ptr == nullptr)<<std::endl;
std::cout<<**ptr<<std::endl;
**ptr = 100;
holder.print();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8bWFwPgoKY2xhc3MgVmFsdWVIb2xkZXIKewoJcHVibGljOgoJCWludCAqKnB1dChjb25zdCBzdGQ6OnN0cmluZyAma2V5KQoJCXsKCQkJLy90aHJvdyBpZiBrZXkgZXhpc3RzCgkJCWF1dG8gdmFsID0gdmFsdWVzLmVtcGxhY2Uoa2V5LCBudWxscHRyKTsKCQkJcmV0dXJuICZ2YWwuZmlyc3QtPnNlY29uZDsKCQl9CgkJdm9pZCBpbml0KCkKCQl7CgkJCWZvciAoYXV0byAmaXQ6IHZhbHVlcykKCQkJCWl0LnNlY29uZCA9IG5ldyBpbnQoMTApOwoJCX0KCQl2b2lkIHByaW50KCkKCQl7CgkJCWZvciAoYXV0byAmaXQ6IHZhbHVlcykKCQkJCXN0ZDo6Y291dDw8Kml0LnNlY29uZDw8c3RkOjplbmRsOwoJCX0KCXByaXZhdGU6CgkJc3RkOjptYXA8c3RkOjpzdHJpbmcsIGludCAqPiB2YWx1ZXM7Cn07CgoKaW50IG1haW4oKSAKewoJVmFsdWVIb2xkZXIgaG9sZGVyOwoJaW50ICoqcHRyID0gaG9sZGVyLnB1dCgiVGVzdCIpOwoJc3RkOjpjb3V0PDwicHRyID09IG51bGxwdHI6ICI8PCAoKnB0ciA9PSBudWxscHRyKTw8c3RkOjplbmRsOwoJCglob2xkZXIuaW5pdCgpOwoJc3RkOjpjb3V0PDwicHRyID09IG51bGxwdHI6ICI8PCAoKnB0ciA9PSBudWxscHRyKTw8c3RkOjplbmRsOwoJc3RkOjpjb3V0PDwqKnB0cjw8c3RkOjplbmRsOwoJCgkqKnB0ciA9IDEwMDsKCWhvbGRlci5wcmludCgpOwoJCglyZXR1cm4gMDsKfQ==