我试图使用
我的程序如下:
for_each
和基于范围的for
循环打印一个二维数组。我的程序如下:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
//for_each (begin(a), end(a), [] (int x) { cout<<x<<" ";}); this code throws error
for_each (begin(a[0]), end(a[2]), [] (int x) { cout<<x<<" ";}); //this code works well, why ?
cout<<endl;
for (auto &row: a) // without & for row, error is thrown
{
for (auto x:row) // no & needed for x, why ?
{
cout<<x<<" ";
}
}
return 0;
}
我的第一个for_each
为什么会报错,为什么在row
上需要使用&
符号?它的类型是什么?row
是一个指针吗?