fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. class Wrapper
  5. {
  6. public:
  7. Wrapper(std::map<int, std::string>& m, const std::string& s) : m(m), s(s) {}
  8.  
  9. void operator =(int k) { m[k] = s; }
  10. private:
  11. std::map<int, std::string>& m;
  12. std::string s;
  13. };
  14.  
  15. class InvertedMap
  16. {
  17. public:
  18. Wrapper operator[](const std::string& s) { return {m, s}; }
  19.  
  20. auto begin() const { return m.begin(); }
  21. auto end() const { return m.end(); }
  22. private:
  23. std::map<int, std::string> m;
  24. };
  25.  
  26.  
  27. int main()
  28. {
  29. InvertedMap Map1;
  30. Map1["Ram"] = 8;
  31. Map1["Aam"] = 8;
  32. Map1["Some"] = 2;
  33. Map1["He"] = 5;
  34. Map1["He"] = 6;
  35.  
  36. for (const auto& p : Map1) {
  37. std::cout << p.second << " " << p.first << std::endl;
  38. }
  39. }
  40.  
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
Some 2
He 5
He 6
Aam 8