
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
using namespace std; int () { int a[] = {1, 2, 3, 4, 5}; int n = sizeof(a) / sizeof(*a); for(int i = 0; i < n; ++i){ cout << a[i] << ' '; } cout << endl;
for(auto i : a){ cout << i << ' '; } cout << endl;
for(int i = 0; i < n; ++i){ int &temp = a[i]; temp = 2 * temp; cout << temp << ' '; } cout << endl;
for(int &temp : a){ temp = 2 * temp; cout << temp << ' '; } cout << endl; return 0; }
|
使用基于范围的for循环,其for循环迭代范围必须是可确定的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
using namespace std;
void func(int a[]){ for(auto i : a){ cout << i << ' '; } cout << endl; return; }
int (){ int a[] = {1, 2, 3, 4, 5}; func(a); return 0; }
|
近期评论