How to Clear Array Element Values in C++

  1. Use Builtin fill() Method to Clear Array Elements in C++
  2. Use std::fill() Algorithm to Clear Array Elements in C++
  3. Use fill_n() Algorithm to Clear Array Elements

This article will introduce multiple methods about how to clear array element values in C++.

Use Builtin fill() Method to Clear Array Elements in C++

The std::array container provides multiple built-in methods that operate on its elements, one of which is the fill() method. It assigns the given value to each element of the array object. Note that you can construct an array with any element type and still use the fill() function to pass the value to assign every member of the container.

#include <iostream>
#include <array>
#include <iterator>
#include <algorithm>

using std::cout; using std::endl;
using std::array; using std::fill;
using std::fill_n;

int main() {
    array<int, 10> nums {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }
    cout << endl;

    nums.fill(0);

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }

    return EXIT_SUCCESS;
}

Output:

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

Use std::fill() Algorithm to Clear Array Elements in C++

Alternatively, we can utilize the std::fill algorithm defined in STL <algorithm> library. This method is called on range based object and assigns a given value to its elements. The range of elements is passed as the first two arguments, and the third one specifies a value to assign. Starting from the C++17 version, the user can also specify the execution policy as an optional argument (see the full details here.

#include <iostream>
#include <array>
#include <iterator>
#include <algorithm>

using std::cout; using std::endl;
using std::array; using std::fill;
using std::fill_n;

int main() {
    array<int, 10> nums {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }
    cout << endl;

    std::fill(nums.begin(), nums.end(), 0);

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }

    return EXIT_SUCCESS;
}

Output:

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

Use fill_n() Algorithm to Clear Array Elements

Another useful method from the <algorithm> header is the fill_n algorithm, which is similar to the above method except that it takes several elements that need to be modified with the given value. In this case, we specified the return value of the size() method to emulate the identical behavior as in the above code example. Note that, execution policy tag applies in this method as well.

#include <iostream>
#include <array>
#include <iterator>
#include <algorithm>

using std::cout; using std::endl;
using std::array; using std::fill;
using std::fill_n;

int main() {
    array<int, 10> nums {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }
    cout << endl;

    std::fill_n(nums.begin(), nums.size(), 0);

    cout << "| ";
    for (const auto &item : nums) {
        cout << item << " | ";
    }

    return EXIT_SUCCESS;
}

Output:

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

Related Article - C++ Array

  • How to Dynamically Allocate an Array in C++
  • How to Copy an Array in C++