fork(2) download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. using namespace std;
  5.  
  6. class A
  7. {
  8. public:
  9. void SetData(std::string key, int value)
  10. {
  11. if(m_data.find(key) != m_data.end()) m_data.at(key) = value;
  12. else m_data.insert(std::make_pair(key,value));
  13. }
  14. int GetData(std::string key)
  15. {
  16. if(m_data.find(key) != m_data.end()) return m_data.at(key);
  17. return 0;
  18. }
  19. void ResetData()
  20. {
  21. for(auto item : m_data)
  22. item.second = 0;
  23. }
  24. void ResetData2()
  25. {
  26. for(auto it=m_data.begin(); it!=m_data.end(); it++)
  27. (*it).second = 0;
  28. }
  29.  
  30. private:
  31. std::map<std::string, int> m_data;
  32. };
  33.  
  34. int main()
  35. {
  36. A a;
  37. a.SetData("KEY1", 10);
  38. std::cout << "Key1: " << a.GetData("KEY1") << std::endl;
  39. a.ResetData();
  40. std::cout << "Key1: " << a.GetData("KEY1") << std::endl;
  41. a.ResetData2();
  42. std::cout << "Key1: " << a.GetData("KEY1") << std::endl;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Key1: 10
Key1: 10
Key1: 0