Search code examples
c++c++11memsetstdarray

Is there std::memset equivalent for std::array?


Is there a memset equivalent for std::array? I think memset is supposed to perform better when initializing an array to zero than looping over the array. I searched one for std::array but could not find anything online.


Solution

  • Use the array's fill() method, that is what its purpose is.

    If you fill it with 0 (for integers, for instance), then the compiler may optimize it if it can (often does, you can look at the generated code).