C++ for-each循环,数组分配在堆上
C++ for-each loop with array allocated on the heap
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
auto arr = new int[5];
// int arr[5] = {1, 2, 3, 4, 5};
for (auto i: arr){
cout << i << ' ';
}
}
为什么这不起作用?
我收到编译时错误提示。
C.cpp: In function 'int main()':
C.cpp:8:15: error: 'begin' was not declared in this scope
for (auto i: arr){
^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
begin(const valarray<_Tp>& __va)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
C.cpp:8:15: error: 'end' was not declared in this scope
for (auto i: arr){
^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note: 'std::end'
end(const valarray<_Tp>& __va)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note: 'std::end'
当我以注释方式初始化数组时,它工作正常。 (明显地)
所以,我认为问题出在新运营商身上。但是我不明白它是什么。
new int[5]
分配一个包含 5 个元素的数组,returns 分配一个指向第一个元素的指针。返回的类型是 rvalue int*
int foo[5]
是一个包含 5 个元素的数组。 foo 的类型是 lvalue int [5]
for 循环的范围要求它知道它正在迭代的对象的大小。它可以遍历数组,但不能遍历指针。考虑这段代码,
int foo[4];
for (int a : (int *)foo) {}
GCC 5.0 也会产生错误"error: ‘begin’ was not declared in this scope",因为数组已衰减为指针。
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
auto arr = new int[5];
// int arr[5] = {1, 2, 3, 4, 5};
for (auto i: arr){
cout << i << ' ';
}
}
为什么这不起作用? 我收到编译时错误提示。
C.cpp: In function 'int main()':
C.cpp:8:15: error: 'begin' was not declared in this scope
for (auto i: arr){
^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
begin(const valarray<_Tp>& __va)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1206:5: note: 'std::begin'
C.cpp:8:15: error: 'end' was not declared in this scope
for (auto i: arr){
^
C.cpp:8:15: note: suggested alternatives:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/x86_64-pc-cygwin/bits/stdc++.h:94:0,
from C.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note: 'std::end'
end(const valarray<_Tp>& __va)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/valarray:1226:5: note: 'std::end'
当我以注释方式初始化数组时,它工作正常。 (明显地) 所以,我认为问题出在新运营商身上。但是我不明白它是什么。
new int[5]
分配一个包含 5 个元素的数组,returns 分配一个指向第一个元素的指针。返回的类型是 rvalue int*
int foo[5]
是一个包含 5 个元素的数组。 foo 的类型是 lvalue int [5]
for 循环的范围要求它知道它正在迭代的对象的大小。它可以遍历数组,但不能遍历指针。考虑这段代码,
int foo[4];
for (int a : (int *)foo) {}
GCC 5.0 也会产生错误"error: ‘begin’ was not declared in this scope",因为数组已衰减为指针。