fork download
  1. #include <iostream>
  2. #include <map>
  3.  
  4. class ReportCard
  5. {
  6. //private: this is the default anyway for a class
  7. public: //made to be able to print the internals below.
  8. std::map<std::string, double> m_report_card;
  9.  
  10. public:
  11.  
  12.  
  13. /* this returns an instance of the std::map. The map is copied and
  14.   returned, so any modifications will not affect m_report_card
  15.   std::map<std::string, double> getReportCardInstance()
  16.   {
  17.   return m_report_card;
  18.   }
  19.  
  20.   if you want to do this, return std::map<std::string, double>&.
  21.   std::map<std::string, double>& getReportCardInstance()
  22.   {
  23.   return m_report_card;
  24.   }
  25.   */
  26.  
  27. // better solution is to have a method to add the report
  28.  
  29. void add_report(const std::string& first,double second)
  30. {
  31. m_report_card[first] = second;
  32. }
  33.  
  34. };
  35.  
  36.  
  37. int main() {
  38. ReportCard rc;
  39. rc.add_report("Percy",1.0);
  40. rc.add_report("Pig",2.0);
  41.  
  42. for(auto internal_report_card : rc.m_report_card)
  43. {
  44. std::cout << internal_report_card.first << ", "
  45. << internal_report_card.second << std::endl;
  46. }
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
Percy, 1
Pig, 2