2013-09-25 61 views
-4
#include <iostream> 

using namespace std; 

int main() 
{ 
    constexpr size_t b = 10; 
    int arr[b]; 
    for (int i = 0; i<b; i++) 
    { 
     arr[i] = i; 
    } 
    for (int x : b) 
    { 
     cout<<x; 
    } 
} 

代码显示在上面。 为什么我不能使用范围for循环打印数组的内容?当我尝试时,它给了我一个错误,说 错误:没有匹配函数调用'开始(const无符号int &)'|错误:无法调用'begin(const unsigned int&)'的匹配函数|

+0

你正在寻找替换第一回路的算法'的std :: iota'。 – chris

回答

7

Why can't I print the contents of an array using the range for loop?

你可以;但幅度数组arr,而不是它的大小b

for (int x : arr) 
      ^^^ 
+0

谢谢!得到它了!不管怎样,为什么我不能使用范围打印b循环? – Slay

+5

因为'b'不是可以作为范围的数组或任何形式的容器。在你的想法中,阅读'for(int x:arr)'为“包含在arr中的所有整数x”。用这个句子中的“b”替换“arr”,你会发现那是没有意义的。 – us2012

相关问题