how to set or initialize default value for all elements of a table or 2d array or multdimentional array

后端 未结 4 833
北恋
北恋 2020-12-11 05:18

I want to set a default nonzero value for all elements of a table or 2d array. array[size]={12} sets first elements only 12 and others are all 0 in a row.But fill(array,arra

4条回答
  •  时光说笑
    2020-12-11 06:17

    For C arrays, you'll probably want to use memset. You've marked this as C++, though, so I feel obliged to give a C++ answer:

    std::vector> v(10, std::vector(10, 45));
    

    This creates a std::vector of 10 std::vectors of size 10 with each element initialized to 45.

    See here for the ideone.

提交回复
热议问题