Loop through Array in C++

C-Plus-Plus-array

In this article, we are going to learn about how to Loop through Array in C++. The array is a collection of elements and we have needs to loop over an array in C++ programs. In this article, we will learn multiple ways to loop over an array in C++.

Ways to loop through array in C++

  • By Using the for Loop through Array in C++
  • By Using Range-based Loop through Array in C++.
  • By Using std::for_each Algorithm to loop through Array in C++.

1. By Using for Loop through Array in C++


The first method that we are going to learn is to iterate over an array by using the simple for loop. Using for loop to iterate over an array is easy as it can make use of the array index. An array has an index starting from 0 for the first element and it goes until the size of array -1

Here is an example to show you the use of for loop to iterate over a character array.

C++ Program to loop through array using for loop

#include <iostream>
using namespace std;

int main()
{
    const char *str[5] = { "One", "Two", "Three", "Four", "Five" };
 
    // We can iterate over it using simple for loop.
    for (int i = 0; i < 5; i++)
        std::cout << str[i] << "\n";
 
    return 0;
}

Output

One
Two
Three
Four
Five

2. Using std::array Loop through array in C++


The same technique can be applied to the std::array class, where we can use the for loop to iterate over each element of the std::array object. Below is another example with the use of std::array class.

C++ Program to loop through array using std::array

#include <iostream>
#include <array>
#include <string>

using namespace std;

int main()
{	 
	std::array<std::string, 5> str = { "One", "Two", "Three", "Four", "Five" };
	
	    // We can iterate over it using simple for loop.
    for (int i = 0; i < str.size(); i++)
	{
        std::cout << str[i] << "\n";
	}
 
    return 0;
}

Output

One
Two
Three
Four
Five

3. By Using the Range-based loop through Array in C++


The range-based loop is the more compact version as compared to the traditional loops. In this, we can access each element of the array even without using the index. We can see the range-based for loop usage in the below example, which is a kind of re-write of the above example.

C++ Program to loop through array using Range-based loop

#include <iostream>
#include <array>

using namespace std;

int main() {
	std::array<std::string, 5> str = { "One", "Two", "Three", "Four", "Five" };
	
    for (auto &item : str) {
        cout << item << endl;
    }
    cout << endl;

    return 0;
}

Output

One
Two
Three
Four
Five

4. Using std::for_each Algorithm & lambda function loop through array


We can make use od std::for_each() algorithm on std::array object. This algorithm helps is iterate over the range of the array object. We can start at the array.begin() and iterate on each element until we reach the array. end(). Then the third argument, we can use a lambda function to add our cout call which actually prints the element for use on the console.

Here is a code example with the above logic in action.

C++ Program to loop through array using std::for_each

#include <iostream>
#include <array>

using namespace std;

int main() {

	std::array<std::string, 5> str = { "One", "Two", "Three", "Four", "Five" };
    array<int, 5> arr = { 1, 2, 3, 4, 5 };
	
	auto printarray = [](auto &item) { cout << item << "\n"; };

    for_each(arr.begin(), arr.end(), printarray);
	
	for_each(str.begin(), str.end(), printarray);
	 
    cout << endl;

    return 0;
}

Output

One
Two
Three
Four
Five