yazmine
-4
Q:

for loop with array c++

#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++;
	}
}
1
for(int i = 0; i < 4; i++) {
  cout << cars[i] << "\n";
}
2
for (int i = 0; i < arr.size(); ++i){
//use if we explicitly need the value of i
cout << i << ":\t" << arr[i] << endl;
}
for (int element : arr){
//modifying element will not affect the array
cout << element << endl;
}
for (int &element : arr){
//modifying element will affect the array
cout << element << endl;
}
1
int v[] = {1,2,3,4,5};
for (int n : v)
  cout << n << endl;
//make sure to compile with -std=c++11
1

New to Communities?

Join the community