fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <utility>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. void foo(T& arg) {
  10. const string key = "value"s;
  11. const auto it = arg.find(key);
  12.  
  13. if(it == arg.end()) {
  14. arg.insert(make_pair(key, 1));
  15. } else {
  16. int prev_value = it->second;
  17. prev_value++;
  18. arg.insert(make_pair(key, prev_value));
  19. }
  20. }
  21.  
  22. int main() {
  23. map<string, long> whatYouDefined;
  24.  
  25. foo(whatYouDefined);
  26.  
  27. cout << "first: " << crbegin(whatYouDefined)->first << " second: " << crbegin(whatYouDefined)->second << endl;
  28.  
  29. map<string, unsigned int> whatYouThoughtYouDefined;
  30.  
  31. foo(whatYouThoughtYouDefined);
  32.  
  33. cout << "first: " << crbegin(whatYouThoughtYouDefined)->first << " second: " << crbegin(whatYouThoughtYouDefined)->second << endl;
  34.  
  35. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
first: value second: 1
first: value second: 1