简体   繁体   中英

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?

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

I suspect that you are trying to use Visual Studio 2012 or earlier. Support for initialization lists on std::map was not added until Visual Studio 2013.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM