如何从地图到达数组的元素?

时间:2019-12-25 16:49:59

标签: c++ arrays dictionary

这是我的密码;

我有这样的地图和数组;

Array dayHours{0,0,0,0,0,0};
Array1 day{"Monday","Tuesday","Wednesday","Thursday","Friday"};
map<pair<string,int>,pair<Array1 ,Array> > matchMap;

我有这样的功能;

void Course::addCourse()
{
    string a;
    int b;
    int total;
    int courseAdd;
    cout<<"How many course you want to add?"<<endl;
    cin>>courseAdd;
    cout<<"Enter the name of course and number of hours"<<endl;
    for(int i=0;i<courseAdd;++i)
    {

        setCourseName(a);
        setHours(b);
        getCourseList().insert({a, b});
        total += getHours();
        matchMap.insert({make_pair(a,b),make_pair(day,dayHours)});
    }

这是地图的打印功能:

void Course::seeMap()
{
    map<pair<string,int>,pair<Array1 ,Array> >::iterator it;
    for(it=matchMap.begin();it!=matchMap.end();++it)
    {
        for(int i=0;i<5;++i)
        {
            cout<<(it->first).first<<(it->first).second<<(it->second).first[i]<<endl;
        }
    }
}

这是我最后得到的:

    How many course you want to add?
2
Enter the name of course and number of hours
Enter the course name
124124124
Enter the course hours
5
Enter the course name
12414124
Enter the course hours
6
You added the courses succesfully!
124124124--->5--->Monday
124124124--->5--->Tuesday
124124124--->5--->Wednesday
124124124--->5--->Thursday
124124124--->5--->Friday
12414124--->6--->Monday
12414124--->6--->Tuesday
12414124--->6--->Wednesday
12414124--->6--->Thursday
12414124--->6--->Friday

我需要的是这样的模板;

Coursename1 ---> Coursehour1 ---> Monday;
Coursename2 ---> Coursehour2 ---> Tuesday;
Coursename3 ---> Coursehour3 ----> wednesday;

... 我如何组织这样的模板的代码?

这里是一个最小的示例,每个人都可以理解我得到的东西。

How many course you want to add?
1
Enter the name of course and number of hours
Enter the course name
Coursenam1
Enter the course hours
4
You added the courses succesfully!
Coursenam1--->4--->Monday
Coursenam1--->4--->Tuesday
Coursenam1--->4--->Wednesday
Coursenam1--->4--->Thursday
Coursenam1--->4--->Friday

1 个答案:

答案 0 :(得分:0)

我更改了这样的代码;

for(int i=0;i<courseAdd;++i)
    {
        setCourseName(a);
        setHours(b);
        getCourseList().insert({a, b});
        total += getHours();
        matchMap.insert({make_pair(a,b),make_pair(day1[i],dayHours)});
    }

void Course::seeMap()
{
    map<pair<string,int>,pair<string ,Array> >::iterator it;
    for(it = matchMap.begin();it!=matchMap.end();++it)
    {
        cout<<(it->first).first<<"--->"<<(it->first).second<<"--->"<<(it->second).first<<endl;
    }
}

然后我得到了想要的模板。

How many course you want to add?
2
Enter the name of course and number of hours
Enter the course name
214124124
Enter the course hours
4
Enter the course name
12512515
Enter the course hours
5
You added the courses succesfully!
12512515--->5--->Tuesday
214124124--->4--->Monday