1array<int, 5> values = {1, 2, 3, 4, 10};
2// the type declaration below must be consistent with the array type
3for (int x : values){ //we use a colon instead of in
4cout << x << endl;
5}
1for (int i=0; i<5; ++i) {
2 cout << i << " ";
3}
4// output is 0 1 2 3 4; note 5 is excluded since < is used and not <=
11 #include <iostream>
22 #include <array>
33 #include <cstdlib>
44
55 using namespace std;
66
77 int main(){
88 array<int, 5> d = {1, 2, -1, 3, 5};
99 cout << "Items before modification: " << endl;
1010 for (int item : d){
1111 cout << item << " ";
1212 }
1313 //multiple elements of d by 3
1414 for (int &itemRef : d){
1515 itemRef *= 3;
1616 }
1717 cout << endl << "Items after modification: " << endl;
1818 for (int item : d){
1919 cout << item << " ";
2020 }
2121 cout << endl;
2222 return 0;
2323 }