Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ statically initialize map<float, float[3]>

So I have a map myMap that I'm trying to statically initialize (has to be done this way).

I'm doing the following:

myMap = 
{
    {415, {1, 52356, 2}}, 
    {256, {356, 23, 6}},
    //...etc
};

However I'm getting the following error: "Array initializer must be an initializer list."

What is wrong with the syntax I have above?

like image 849
user1855952 Avatar asked Nov 27 '13 11:11

user1855952


1 Answers

You should use array<float, 3> instead of "plain" arrray:

#include <map>
#include <array>
#include <iostream>

int main()
{
    std::map<float, std::array<float, 3>> myMap
    {
        {415, std::array<float, 3>{1, 52356, 2}},
        {256, std::array<float, 3>{356, 23, 6}}
        //...etc
    };

    /* OR 

    std::map<float, std::array<float, 3>> myMap
    {
        {415, {{1, 52356, 2}}},
        {256, {{356, 23, 6}}}
        //...etc
    };

    */

    std::cout << myMap[415][0] << " " << myMap[256][1] << " " << std::endl;

    return 0;
}
like image 98
Nemanja Boric Avatar answered Nov 15 '22 18:11

Nemanja Boric