首页 > 解决方案 > 如何像模板一样打印地图?

问题描述

这是我的代码:

为了理解代码,我必须解释一下。首先,我从不同班级的用户那里获取学生和课程数据。然后在 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

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

标签: c++dictionary

解决方案


推荐阅读