如何像模板一样打印地图?

时间:2019-12-29 18:53:24

标签: c++ dictionary

这是我的代码:

要理解代码,我必须解释一下。首先,我从不同班级的用户那里获取学生和课程数据。然后在Schedule类中,我试图为学生创建一个时间表。当用户输入有效的学生和课程名称时,我会将这些数据插入另一张地图。

map< string,map<pair<string,int>,pair<string,Array> > >  courseScList;
map<pair<string,int>,pair<string,Array> > matchMap;

matchMap映射具有来自另一个类的这些数据:

course name,course hours,course day,available hours in this day

这两个是我的地图,我在以下代码中使用这些地图:

void Schedule::studentSchedule() {
    string c,s;
    int courseNum;
    list<string>::iterator studentLoc;
    map< string,map<pair<string,int>,pair<string,Array> > >::iterator last;
    map<pair<string,int>,pair<string,Array> >::iterator location;
    map<pair<string,int>,pair<string ,Array> >::iterator match;
    dummy1:
    cout<<"Enter the student name"<<endl;
    cin >> s;
    wrongCourse:
    cout<<"Enter the course names you want"<<endl;
    cin>>c;

    auto predicate = [&](auto& course) { return compareName(course, c); };
    studentLoc = find(getStudentList().begin(),getStudentList().end(),s);
    location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
    if(studentLoc != getStudentList().end() || location != getMatchMap().end())
    {
        getCourseScList().insert(make_pair(s,getMatchMap()));
    }
    else
        {
        cout<<"The course or student you wrote isn't available.Please enter existed courses!"<<endl;
        goto wrongCourse;
        }
    cout<<"\n"<<endl;
    char dummy;
    cout<<"\nDo tou want to create a new schedule?Press y or n."<<endl;
    cin>>dummy;
    if(dummy == 'Y' || dummy == 'y')
        goto dummy1;
    else
    {
        location = last->second.begin();
        for(last = getCourseScList().begin();last!=getCourseScList().end();++last)
        {
                cout<<last->first<<"\t"<<(location->first).first<<"\t"<<(location->second).first<<"\t";
                for (const int element : location->second.second)
                    std::cout << element << " ";
                cout<<endl;
            ++location;
        }

    }

我正试图从用户那里获取学生和课程名称,如果学生名称和课程名称在地图中,我要将其插入到另一张地图中。但是,当我尝试打印这些学生,课程,日数和日数时,我无法在同一行中看到它们。我认为这是错误的代码:

 else
            {
            cout<<"The course or student you wrote isn't available.Please enter existed courses!"<<endl;
            goto wrongCourse;
            }
        cout<<"\n"<<endl;
        char dummy;
        cout<<"\nDo tou want to create a new schedule?Press y or n."<<endl;
        cin>>dummy;
        if(dummy == 'Y' || dummy == 'y')
            goto dummy1;
        else
        {
            location = last->second.begin();
            for(last = getCourseScList().begin();last!=getCourseScList().end();++last)
            {
                    cout<<last->first<<"\t"<<(location->first).first<<"\t"<<(location->second).first<<"\t";
                    for (const int element : location->second.second)
                        std::cout << element << " ";
                    cout<<endl;
                ++location;
            }

        }

在这个打印循环中,我看不到这样的模板;

Studentname1  -- > Coursename1 ----> Courseday1 -->Coursehours1
Studentname1  -- > Coursename2 ----> Courseday2 -->Coursehours1
Studentname2  -- > Coursename1 ----> Courseday1 -->Coursehours1
Studentname3  -- > Coursename2 ----> Courseday2 -->Coursehours

这只是我想做的一个例子。如何组织代码?

0 个答案:

没有答案