1

Here is my question.

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

I have private data like this.

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;
    }
}

I this function I want to create a map with days and empty array.

When I do that I am getting an error like this.

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));

Can you help me about that?

JeJo
  • 30,635
  • 6
  • 49
  • 88
themmfa
  • 499
  • 4
  • 15

2 Answers2

2

The 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. Therefore you need to provide map's key as an int*.:

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

However, you can use std::array instead of plain arrays.

#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

As a side note, do not practice with “using namespace std;. See the following post for more info: Why is "using namespace std;" considered bad practice?

JeJo
  • 30,635
  • 6
  • 49
  • 88
1

Adding on to @JeJo's answer, if you really want to use static arrays, then you'd need to declare their fixed size beforehand. A good alternative to this is to use vector that are essentially dynamic arrays.

#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;
    }
}

Output

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 
Ajay Dabas
  • 1,404
  • 1
  • 5
  • 15