如何将空数组传递给地图?

How to pass an empty array to a map?

这是我的问题。

private:
map<string,int &> days;
int daysHours[6] = {};

我有 private 这样的数据。

void Schedule::studentSchedule()
{
    getDays().insert(pair<string, int&>("Monday", daysHours));
    getDays().insert(pair<string, int& >("Tuesday", daysHours));
    getDays().insert(pair<string, int&>("Wednesday", daysHours));
    getDays().insert(pair<string, int& >("Thursday", daysHours));
    getDays().insert(pair<string, int&>("Friday", daysHours));

    map<string, int&>::iterator it;
    for (it = getDays().begin(); it != getDays().end(); ++it)
    {
        cout << "Day: " << it->first << "\tHours: " << it->second << endl;
    }
}

在这个函数中,我想创建一个包含天数和空数组的地图。

当我这样做时,出现了这样的错误。

error: no matching function for call to 'std::pair<std::__cxx11::basic_string<char>, int&>::pair(const char [7], int [6])'
 getDays().insert(pair<string,int &>("Monday",daysHours));

你能帮我一下吗?

plain arrays decay to pointers of the array's type, not the references. Meaning daysHours decays to int* not int&. That was the reason for the compiler error See here live。因此,您需要提供地图的密钥作为 int*.:

std::map<string, int*> days;
//              ^^^^

但是,您可以使用 std::array 而不是普通数组。

#include <map>
#include <array>


using Array = std::array<int, 6>;  // convenience type
std::map<std::string, Array> days;
Array daysHours{ 0, 0, 0, 0, 0, 0 };

days.emplace("Monday", daysHours);  // constrcut in place
//... rest

附带说明一下,不要用“using namespace std;练习。有关详细信息,请参阅以下 post: Why is "using namespace std;" considered bad practice?

加上@JeJo 的回答,如果你真的想使用静态数组,那么你需要事先声明它们的固定大小。 一个很好的替代方法是使用本质上是动态数组的 vector

#include <bits/stdc++.h> 
using namespace std;

int main(){
    cout<<"Using static arrays\n";
    map<string,int (*)[6]> days;
    int daysHours[6] = {0,1,2,3,4,5};
    days.insert(pair<string,int (*)[6]>("Monday",&daysHours));
    days.insert(pair<string,int (*)[6] >("Tuesday",&daysHours));
    days.insert(pair<string,int (*)[6]>("Wednesday",&daysHours));
    days.insert(pair<string,int (*)[6] >("Thursday",&daysHours));
    days.insert(pair<string,int (*)[6]>("Friday",&daysHours));
    map<string,int (*)[6]>::iterator it;
    for(it=days.begin();it!=days.end();++it)
    {
        cout<<"Day: "<<it->first<<"\tHours: ";
        for(int i=0;i<6;i++){
            cout<<*(*(it->second)+i)<<" ";
        }
        cout<<endl;
    }
    cout<<"Alternative solution: Use vectors(dynamic arrays)\n";
    map<string,vector<int>> days2;
    vector<int> daysHours2{0,1,2};
    days2.insert(pair<string,vector<int>>("Monday",daysHours2));
    days2.insert(pair<string,vector<int> >("Tuesday",daysHours2));
    days2.insert(pair<string,vector<int>>("Wednesday",daysHours2));
    days2.insert(pair<string,vector<int> >("Thursday",daysHours2));
    days2.insert(pair<string,vector<int>>("Friday",daysHours2));
    map<string,vector<int>>::iterator it2;
    for(it2=days2.begin();it2!=days2.end();++it2)
    {
        cout<<"Day: "<<it2->first<<"\tHours: ";
        for(auto i:it2->second){
            cout<<i<<" ";
        }
        cout<<endl;
    }
}

输出

Using static arrays
Day: Friday     Hours: 0 1 2 3 4 5 
Day: Monday     Hours: 0 1 2 3 4 5 
Day: Thursday   Hours: 0 1 2 3 4 5 
Day: Tuesday    Hours: 0 1 2 3 4 5 
Day: Wednesday  Hours: 0 1 2 3 4 5 
Alternative solution: Use vectors(dynamic arrays)
Day: Friday     Hours: 0 1 2 
Day: Monday     Hours: 0 1 2 
Day: Thursday   Hours: 0 1 2 
Day: Tuesday    Hours: 0 1 2 
Day: Wednesday  Hours: 0 1 2