fork download
  1. #include <map>
  2. #include <list>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <vector>
  6.  
  7. int getage(const std::pair<std::string, int>& pair)
  8. {
  9. return pair.second;
  10. }
  11.  
  12. int main()
  13. {
  14. std::map<std::string, int> agemap;
  15. agemap["jill"] = 13;
  16. agemap["jack"] = 31;
  17.  
  18. std::list<int> agelist(agemap.size());
  19. std::transform(agemap.begin(), agemap.end(), agelist.begin(), getage);
  20.  
  21. // or:
  22. std::vector<int> v;
  23. std::transform(agemap.begin(), agemap.end(), std::back_inserter(v), getage);
  24.  
  25. // By popular demand, and just to spell it out:
  26. int age_array[10];
  27. std::transform(agemap.begin(), agemap.end(), age_array, getage);
  28.  
  29. // or even
  30. int *dyn_array = new int[agemap.size()];
  31. std::transform(agemap.begin(), agemap.end(), dyn_array, getage);
  32.  
  33. delete[] dyn_array;
  34.  
  35. }
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout

Standard output is empty