C++ Program to iterate over each element from the arrays


A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain features in order to insert, delete, traverse, and update elements effectively, just like other data structures do. Our arrays in C++ are static. In addition, C++ offers a few dynamic array structures. There may be a maximum of Z elements that can be stored inside a static array. And there are currently n elements in it. In this article, we will see how to iterate over all elements present inside the array using C++.

Understanding the concept with examples

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
Read all elements one by one
10
14
65
85
96
12
35
74
69

To iterate over all elements from an array, we can simply run a loop and access the indices one by one and display the content of the array. There is no such simpler method except this. So the algorithm is also straightforward, as shown here.

Algorithm

  • Take the array A of size n

  • for index i ranging from 0 to n - 1, do

    • display A[ i ]

  • end for

Example

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << "Index: " << i << " Element: " << arr[ i ] << endl;
   } 
} 

int main() {
   int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array elements: " << endl;
   displayArr( arr, n );
}

Output

Array elements: 
Index: 0 Element: 57
Index: 1 Element: 10
Index: 2 Element: 14
Index: 3 Element: 19
Index: 4 Element: 86
Index: 5 Element: 52
Index: 6 Element: 32
Index: 7 Element: 14
Index: 8 Element: 76
Index: 9 Element: 65
Index: 10 Element: 32
Index: 11 Element: 14

Using Vectors iterator

Unlike previous static array methods, we can use vectors. The vectors are dynamic arrays. The dynamic arrays may increase their size whenever needed. We can also the same method as before for the vectors as well. But there is another better method, which uses iterator classes. The iterators also come with C++ STL. Let us see the code to use iterators to access vector elements.

Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   vector<int>::iterator it;
   for( it = v.begin();  it != v.end() ; it++ ){
      cout << "Index: " << (it - v.begin()) << " Element: " << *it << endl;
   } 
}

int main() {
   vector<int> arr = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Array elements: " << endl;
   displayArr( arr );
}

Output

Array elements: 
Index: 0 Element: 57
Index: 1 Element: 10
Index: 2 Element: 14
Index: 3 Element: 19
Index: 4 Element: 86
Index: 5 Element: 52
Index: 6 Element: 32
Index: 7 Element: 14
Index: 8 Element: 76
Index: 9 Element: 65
Index: 10 Element: 32
Index: 11 Element: 14

In this method, the iterator object is created which is assigned to v.begin() which is the starting position of the vector. When it is not v.end() print the (it – v.begin()) this returns the index of the element (because ‘it’ is the address of the current element and v.begin() is the address of the first element. So the difference is the index). The value of ‘it’ can be accessed with *it.

Using Vectors and for-each loop

This method is not different than any other method shown above. The only difference is in the loop. Later C++ versions support for-each loop where we loop through the elements implicitly, not through the index. Let us see the code example for a clear understanding.

Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){ 
   for( int e : v ){
      cout << "Element: " << e << endl;
   } 
}

int main() {
   vector<int> arr = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; 
   
   cout << "Array elements: " << endl;
   displayArr( arr );
}

Output

Array elements: 
Element: 57
Element: 10
Element: 14
Element: 19
Element: 86
Element: 52
Element: 32
Element: 14
Element: 76
Element: 65
Element: 32
Element: 14

Conclusion

In this article, we have seen two different approaches to reading all elements from an array. The first is using static arrays where we are going through each element one by one by accessing indices. The next method is using vector iterators. Vector iterators are like any other iterators like lists, maps, sets, etc. Which can be used to get the elements one by one using loops. In later versions of C++, we can get another interesting for loop which is the for-each loop. This does not require additional index or pointer references. It is shown in the last example in this document.

Updated on: 13-Dec-2022

809 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements