What is the safe way to fill multidimensional array using std::fill?

前端 未结 5 1851
终归单人心
终归单人心 2020-11-30 04:40

Here is what I am using:

class something
{
   char flags[26][80];
} a;

std::fill(&a.flags[0][0], &a.flags[0][0] + 26 * 80, 0);

(Upda

5条回答
  •  一向
    一向 (楼主)
    2020-11-30 04:41

    What is the safe way to fill multidimensional array using std::fill?

    The easy default initialization would be using braced inilization.

    char flags[26][80]{};
    

    The above will initialize all the elements in the flags to default char.


    2-D Array filling using std::fill or std::fill_n

    However, in order to provide different value to initialize the above is not enough. The options are std::fill and std::fill_n. (Assuming that the array flags is public in your class)

    std::fill(
       &a.flags[0][0],
       &a.flags[0][0] + sizeof(a.flags) / sizeof(a.flags[0][0]),
       '0');
    
    // or using `std::fill_n`
    // std::fill_n(&a.flags[0][0], sizeof(a.flags) / sizeof(a.flags[0][0]), '1');
    

    To generalize this for any 2d-array of any type with any initializing value, I would suggest a templated function as follows. This will also avoid the sizeof calculation of the total elements in the array.

    #include  // std::fill_n, std::fill
    #include    // std::size_t
    
    template
    constexpr void fill_2D_array(Type(&arr2D)[M][N], const Type val = Type{}) noexcept
    {
       std::fill_n(&arr2D[0][0], M * N, val);
       // or using std::fill
       // std::fill(&arr2D[0][0], &arr2D[0][0] + (M * N ), val);
    }
    

    Now you can initialize your flags like

    fill_2D_array(a.flags, '0'); // flags should be `public` in your class!
    

    (See Live Online)


    3-D Array filling using std::fill or std::fill_n

    Adding one more non-template size parameter to the above template function, this can be brought to 3d-arrays as well

    #include  // std::fill_n
    #include    // std::size_t
    
    template
    constexpr void fill_3D_array(Type(&arr3D)[M][N][O], const Type val = Type{}) noexcept
    {
       std::fill_n(&arr3D[0][0][0], M * N * O, val);
    }
    

    (See Live Online)

提交回复
热议问题