Set an array to zero with c++11

You might use std::fill:

std::fill(std::begin(array), std::end(array), 0);

For a C style array such as int array[100] you can use std::fill as long as array is an array. A pointer to the array will not work.

std::fill(std::begin(array), std::end(array), 0);

If you are using a pointer to the first element, you must supply the size of your array yourself.

std::fill(array, array + size, 0);

In C++, it's recommended to use std::array instead of C style arrays. For example, you could use std::array<int, 100> foo; instead of int foo[100]; std::array always knows its size, doesn't implicitly decay to a pointer and has value semantics. By using std::array you can simply reset the array with :

foo.fill(0);

or

foo = {};

Tags:

C++

Arrays

C++11