How to Empty an Array in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, arrays are fixed-size containers that store elements of the same data type. Empty an array means removing all elements from it. In this article, we will see how to empty an array in C++. Example: Input: myArray = { 1, 8, 2, 9, 1, 5, 6 } Output: myArray = { 0, 0, 0, 0, 0, 0, 0 }Clear an Array in C++In C++, there is no direct function to empty an array, but we can achieve this by using the std::fill() method from the <algorithm> library to set each element to its initial state. Syntax of std::fill()std::fill (arr, arr+n, defaultValue);Here, arr: Pointer to the first element of the array.arr + n: Pointer to the hypothetical element after the last element of the array.defaultValue: value to be set.C++ Program to Empty an ArrayThe below program demonstrates how we can empty an array in C++ using the fill method. C++ // C++ Program to illustrate how to empty an array #include <algorithm> #include <iostream> using namespace std; int main() { // Initialize an array int arr[] = { 10, 20, 30, 40, 50 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Empty the array by setting each element to 0 using // fill fill(arr, arr + n, 0); cout << "Array after emptying: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } OutputOriginal array: 10 20 30 40 50 Array after emptying: 0 0 0 0 0 Time Complexity: O(N), where N is the size of the array.Auxiliary Space: O(1) Note: We can also use memset() function to empty an array in C++. Comment More infoAdvertise with us Next Article How to Empty an Array in C++? M maha123 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Check if an Array is Empty in C++? In C++, arrays are fixed-size data structures that allow the users to store similar data in contiguous memory locations. In this article, we will learn how to check if an array is empty in C++. Example: Input:arr[]={}arr[]={1,2,3,4,5}Output:The array is emptyThe array is not emptyCheck if an Array i 3 min read How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Deallocate a 2D Array in C++? In C++, an array of arrays is called a 2D array, or two-dimensional array. Deallocating a 2D array means freeing the allocated memory to prevent any memory leaks, especially when working with dynamically allocated memory. In this article, we will learn how to deallocate a 2D array in C++. Deallocati 3 min read How to Remove an Element from Array in C++? In C++, removing an element from an array is tricky because we cannot modify the memory space occupied by the array. So, we pseudo delete the element by moving it to the end of the array and reducing the size. In this article, we will learn how to remove a value from an array in C++.ExamplesInput: a 3 min read How to Create a Deque of Arrays in C++? In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends whereas arrays are fixed-size collections of elements. In this article, we will learn how to create a deque of arrays in C++ STL. Example: Input: myArray1 = {1, 4, 8, 9, 11} myArray2 = {1, 2, 3, 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read Like