looping through array in c++ code example

Example 1: c++ loop through array

string texts[] = {"Apple", "Banana", "Orange"};
for( unsigned int a = 0; a < sizeof(texts); a = a + 1 )
{
    cout << "value of a: " << texts[a] << endl;
}

Example 2: c++ loop through int array

#include <iostream>
#include <array>

int main()
{
	int aNumbers[] = { 0, 1, 2, 3, 4, 5 };
	int count = 0;	
	
	for (int aNumber : aNumbers)
	{		
		std::cout << "Element "<< count << " : " << aNumber << std::endl;
		count++;
	}
}

Example 3: how to iterate through array in c++

for(int i=0; i<n; i++)
  cout<<array[i]<<" "
  cout<<endl;

Tags:

Cpp Example