How to Loop Over an Array in C++? Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report 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: 1 2 3 4 5Loop Through Each Item of an Array in C++For iterating through an array, we create a loop that iterates the same number of times as there are elements in the array. For that, we will create a loop variable that starts from 0 (as arrays in C++ are 0-indexed), increments by one in each iteration and goes till it is less than the size of the array. We can use any loop of our choice. Here, we are using for loop Syntax for Iterating Through an Array in C++Below is the syntax of a basic for loop to traverse over an array. for (lv; lv < size_of_array; lv++) { //body of loop }where, lv: It represents the loop variable.C++ Program to Loop Over an ArrayThe below example demonstrates how we can loop over an array in C++. C++ // C++ program to demonstrate how we can loop over an array #include <iostream> using namespace std; int main() { // Create and initialize an array int arr[] = { 1, 2, 3, 4, 5 }; // calculate the size of an array int n = sizeof(arr) / sizeof(arr[0]); cout << "Elements in an Array: "; // Loop over an array and print each element for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } OutputElements in an Array: 1 2 3 4 5 Time Complexity: O(n), here n is a size of an array.Auxiliary Space: O(1) Note: Besides using traditional loops, we can also use the range-based for loops and std::for_each algorithm to loop over an array and access each element. Comment More infoAdvertise with us Next Article How to Loop Over an Array in C++? R rahulkatix28 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin 4 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 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 How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read How to Resize an Array of Strings in C++? In C++, the array of strings is useful for storing many strings in the same container. Sometimes, we need to change the size of this array. In this article, we will look at how to resize the array of strings in C++. Resize String Array in C++There is no way to directly resize the previously allocate 2 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9}; 2 min read How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w 2 min read How to Copy One Array to Another in C++? In C++, arrays are a type of data structure that stores a fixed-size collection of elements of the same type in a contiguous memory location, and sometimes we need to copy the contents of one array to another. In this article, we will learn how to copy one array to another in C++. Example: Input: ar 2 min read Like