How to initialize all elements in an array to the same number in C++

后端 未结 16 2002
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 07:17

I\'m trying to initialize an int array with everything set at -1.

I tried the following, but it doesn\'t work. It only sets the first value at -1.

in         


        
16条回答
  •  时光说笑
    2020-12-05 08:05

    I had the same question and I found how to do, the documentation give the following example :

    std::array a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
    

    So I just tried :

    std::array a1{ {1} }; // double-braces required in C++11 (not in C++14)
    

    And it works all elements have 1 as value. It does not work with the = operator. It is maybe a C++11 issue.

提交回复
热议问题