mercredi 28 août 2019

How to insert to C++11 stdmap?

I am trying to insert a set of pair values into a std::map in C++11. However the values don't seem to insert into the std::map. Please do go over my code about the same. I appreciate any and all help.

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cstdlib>
    #include<utility>
    #include<ctime>

    #include "print.h"

    class ReportCard
    {
       private:
       std::map<std::string, double> m_report_card;

       public:

    std::map<std::string, double> getReportCardInstance()
    {
        return m_report_card;
    }

    };


    class Student
    {

      private:
        int m_roll_no;
        std::string m_name;

        ReportCard m_reportCard;

        public:
              Student(int inRollNo, const std::string& inName) : 
        m_roll_no(inRollNo), m_name(inName)
    {

    }

    std::string getName()
    {
        return m_name;
    }

    int getRollNo()
    {
        return m_roll_no;
    }

    ReportCard getReportCard()
    {
        return self.m_reportCard;
    }

    int getReportCardSize()
    {
        return m_reportCard.getReportCardInstance().size();
    }
    };

    class Driver
    {
        private:
          std::vector<Student> student_list;
          std::vector<Student> temp;

        public:
          void studentTestPopulate()
          {
             student_list.push_back(Student(1, "Tim"));
             student_list.push_back(Student(2, "Matt"));
             student_list.push_back(Student(100, "Luke"));
             student_list.push_back(Student(68, "Lissy"));
             student_list.push_back(Student(20, "Tony"));
             student_list.push_back(Student(33, "Joseph"));
             student_list.push_back(Student(14, "Sid"));
             student_list.push_back(Student(15, "Roby"));
             student_list.push_back(Student(44, "Rohan"));
             student_list.push_back(Student(11, "Kevin"));
             student_list.push_back(Student(19, "George"));
        }


        void reportCardPopulate()
        {
            for (auto& student : student_list)
            {
            std::cout << student.getName() << std::endl;

 student.getReportCard().getReportCardInstance().insert(std::make_pair<std::string, double>("Math", generateMark));

//This is the function that does not work. No marks are printed!!
            for (auto& mark : student.getReportCard().getReportCardInstance())
            {
                std::cout << mark.first << " " << mark.second;
            }

            //student.getReportCard().getReportCardInstance().insert(std::make_pair("Science", generateMark));
            //student.getReportCard().getReportCardInstance().insert(std::make_pair("Geography", generateMark));
            //student.getReportCard().getReportCardInstance().insert(std::make_pair("French", generateMark));
            //student.getReportCard().getReportCardInstance().insert(std::make_pair("History", generateMark));
        }
    }

    void showAllStudentDetails()
    {
        for (auto& student : student_list)
        {
            std::cout << student.getName() << std::endl;
            std::cout << student.getRollNo() << std::endl;

            std::cout << "REPORT CARD : " << student.getReportCardSize() << std::endl << std::endl;

            for (auto& mark : student.getReportCard().getReportCardInstance())
            {
                std::cout << mark.first << std::endl;
                std::cout << mark.second << std::endl;
            }
        }
    }
};

int main()
{
    srand(time(NULL));

    Driver driver;
    driver.studentTestPopulate();
    driver.reportCardPopulate();
    //driver.showAllStudentDetails();
}

The reportCardPopulate() function is supposed to insert pairs of values into a report_card map. However, the insert function doesn't seem to work. When we try to print the values within the reportCardPopulate() function, it doesn't print anything. When I try to print the size of the map, it prints 0. When I printed the size using sizeof() it prints the same size before and after the insertion.

Aucun commentaire:

Enregistrer un commentaire