fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. class ValueHolder
  6. {
  7. public:
  8. int **put(const std::string &key)
  9. {
  10. //throw if key exists
  11. auto val = values.emplace(key, nullptr);
  12. return &val.first->second;
  13. }
  14. void init()
  15. {
  16. for (auto &it: values)
  17. it.second = new int(10);
  18. }
  19. void print()
  20. {
  21. for (auto &it: values)
  22. std::cout<<*it.second<<std::endl;
  23. }
  24. private:
  25. std::map<std::string, int *> values;
  26. };
  27.  
  28.  
  29. int main()
  30. {
  31. ValueHolder holder;
  32. int **ptr = holder.put("Test");
  33. std::cout<<"ptr == nullptr: "<< (*ptr == nullptr)<<std::endl;
  34.  
  35. holder.init();
  36. std::cout<<"ptr == nullptr: "<< (*ptr == nullptr)<<std::endl;
  37. std::cout<<**ptr<<std::endl;
  38.  
  39. **ptr = 100;
  40. holder.print();
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
ptr == nullptr: 1
ptr == nullptr: 0
10
100