【问题标题】:How to reach array's elements from map?如何从地图中获取数组的元素?
【发布时间】:2019-12-25 16:49:59
【问题描述】:

这是我的代码;

我有这样的地图和数组;

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

【问题讨论】:

  • 或者是否有可能得到这个带有列表或向量的模板?
  • 请做一个最小的可运行示例
  • 我添加了关于我得到什么的简单示例
  • 这是你说的吗?

标签: c++ arrays dictionary


【解决方案1】:

我像这样更改了代码;

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

【讨论】:

    猜你喜欢
    • 2019-12-17
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2021-01-07
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多