fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5.  
  6. class Package {
  7. public:
  8. double val;
  9. double calculate_cost(){
  10. return this->val;
  11. }
  12. Package(double val):val(val){}
  13. };
  14.  
  15. int main(){
  16. std::map<std::string, std::vector<Package*>> packagemap;
  17.  
  18. //dados de teste
  19. std::vector<Package*> package1;
  20. package1.push_back(new Package(10));
  21. package1.push_back(new Package(15));
  22. package1.push_back(new Package(13));
  23. packagemap.insert(std::make_pair("test1", package1));
  24.  
  25.  
  26. std::vector<Package*> package2;
  27. package2.push_back(new Package(20));
  28. package2.push_back(new Package(35));
  29. package2.push_back(new Package(56));
  30. packagemap.insert(std::make_pair("test2", package2));
  31.  
  32. //calcular o total
  33. double total = 0;
  34. for (auto package_entry : packagemap){
  35. for (auto package_ptr: package_entry.second){
  36. total += package_ptr->calculate_cost();
  37. }
  38. }
  39.  
  40. std::cout << total;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
149