James James - 3 months ago 31
C++ Question

c++ (not >=11), initializing nested pairs in a map

Under the restriction that I should not compile with -std=c++11, I want to add an additional variable to the following variable,

std::map<unsigned,
std::map<unsigned,
std::pair<
std::pair<int, int>,
std::pair<bool, bool>
>
>
>
temp;


and accordingly adjust its initialization:

for (int i=0; i<100; ++i)
{
temp[i][i]=
std::pair<
std::pair<int, int>,
std::pair<bool, bool>
>
> (
std::pair<int, int> (intsX[i], intsY[i]),
std::pair<bool, bool> (boolsX[i], boolsY[i])
);
}


I would like to change the structure to this:

std::map<unsigned,
std::map<unsigned,
std::pair<
std::pair<int, int>,
std::pair<
std::pair<bool, bool>,
double
>
>
>
>
temp;


Now, I am not sure how to change the initialization, for example ...

for (int i=0; i<100; ++i)
{
temp[i][i]=
std::pair<
std::pair<int, int>,
std::pair<
std::pair<bool, bool>,
double
>
> (
std::pair<int, int> (intsX[i], intsY[i]),
std::pair<std::pair, double> (
std::pair<bool, bool> (boolsX[i], boolsY[i]),
doublesX[i]
)
);
}


I can use c++11 initialization lists as follows however is it possible to do this without using this c++11 feature(?):

for (int i=0; i<100; ++i)
{
temp[i][i]=
std::pair<
std::pair<int, int>,
std::pair<
std::pair<bool, bool>,
double
>
> (
std::pair<int, int> (intsX[i], intsY[i]),
std::pair<std::pair<bool, bool>, double> ({boolsX[i], boolsY[i]}, doublesX[i])
);
}

Answer

Use std::make_pair to make the code more legible.

temp[i][i] = std::make_pair(
    std::make_pair(intsX[i], intsY[i]),
    std::make_pair(
        std::make_pair(boolsX[i], boolsY[i]),
        doublesX[i])));