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

后端 未结 16 1800
伪装坚强ぢ
伪装坚强ぢ 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 07:55

    Can't do what you're trying to do with a raw array (unless you explicitly list out all 100 -1s in the initializer list), you can do it with a vector:

    vector directory(100, -1);
    

    Additionally, you can create the array and set the values to -1 using one of the other methods mentioned.

提交回复
热议问题