6

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?

5
  • Check this. stackoverflow.com/questions/2172053/… Commented Nov 27, 2013 at 11:51
  • I have checked that out and I don't think I'm having the same issue because my attempts to statically initialize a map of type map<float, float> instead of map<float, float[3]> works just fine. I only get this issue when the value is an array Commented Nov 27, 2013 at 11:53
  • please check this stackoverflow.com/questions/138600/… Commented Nov 27, 2013 at 11:54
  • The problem only occurs when I'm trying to do it this way with an array for the value. However what I'm doing above is essentially what they say to do in the link that you posted Commented Nov 27, 2013 at 11:55
  • 1
    This link should be useful. cplusplus.com/forum/beginner/95335 Commented Nov 27, 2013 at 11:59

2 Answers 2

4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.