为什么我不能在C ++中的int指针数组中使用a for each循环

时间:2020-12-27 22:46:12

I'm trying to have an array of pointers (int *a[10]) and then use a range based for loop (as in the C++11 standard). However, the compiler complains D: - it says "error: invalid initialization of reference of type ‘int&’ from expression of type ‘int*’".

我正在尝试使用一个指针数组(int * a [10]),然后使用基于for循环的范围(如C ++ 11标准)。但是,编译器抱怨D: - 它表示“错误:从类型'int *'的表达式中无效初始化类型'int&'的引用”。

My code is basically:

我的代码基本上是:

main(){
    int *a[10];
    for(int& e : a){}
}

I had also tried: //... for(int* e : a){}

我也尝试过:// ... for(int * e:a){}

But having neither, inside the loop body, *e = 1 or e = 1 works.

但是,在循环体内,* e = 1或e = 1都没有。

I've read this question which links me to this page; by reading that, I think I understand that the container you're trying to iterate over must implement a begin() and an end() methods. I guess the primitive arrays don't implement these; is that the case? Also, is it the case that int& a = c; makes a have the same address as c, for some int c?

我已经阅读了这个将我链接到此页面的问题;通过阅读,我想我明白你试图迭代的容器必须实现begin()和end()方法。我猜原始数组不实现这些;是这样吗?还有,int&a = c;对于一些int c,使得与c具有相同的地址?

Is there another alternative?

还有另一种选择吗?

2 个解决方案

#1


3  

*e = 1 suggests that you want to make 10 pointers to 10 integers, each of which has the value 1.

* e = 1表示你想要10个指针指向10个整数,每个整数的值为1。

First, you need to have a loop that references all the 10 pointers:

首先,你需要一个引用所有10个指针的循环:

int* a[10];
for(int*& e : a) // Reference so you can modify a. 

Then, you need to create 10 pointers:

然后,您需要创建10个指针:

  e = new int;

and those have to be pointers to the value 1 so we complete the statement

那些必须指向值1,所以我们完成了声明

 *e = 1;

or shorter

  e = new int (1);

When cleaning up, you'd have a similar loop:

清理时,你会有一个类似的循环:

 for (int& e : a) {
      delete e;
      e = nullptr;
 }

#2


2  

It's an array of int*, so of course you can't iterate over it using a variable of type int, that isn't the type of object in the array!

它是一个int *数组,所以你当然不能使用int类型的变量迭代它,这不是数组中对象的类型!

The objects in the array are int* so that's what your loop variable needs to be.

数组中的对象是int *,这就是你的循环变量需要的东西。

for (int* e : a) ...

Or

for (int*& e : a) ...

Or simply:

for (auto& e : a) ...

#1


3  

*e = 1 suggests that you want to make 10 pointers to 10 integers, each of which has the value 1.

* e = 1表示你想要10个指针指向10个整数,每个整数的值为1。

First, you need to have a loop that references all the 10 pointers:

首先,你需要一个引用所有10个指针的循环:

int* a[10];
for(int*& e : a) // Reference so you can modify a. 

Then, you need to create 10 pointers:

然后,您需要创建10个指针:

  e = new int;

and those have to be pointers to the value 1 so we complete the statement

那些必须指向值1,所以我们完成了声明

 *e = 1;

or shorter

  e = new int (1);

When cleaning up, you'd have a similar loop:

清理时,你会有一个类似的循环:

 for (int& e : a) {
      delete e;
      e = nullptr;
 }

#2


2  

It's an array of int*, so of course you can't iterate over it using a variable of type int, that isn't the type of object in the array!

它是一个int *数组,所以你当然不能使用int类型的变量迭代它,这不是数组中对象的类型!

The objects in the array are int* so that's what your loop variable needs to be.

数组中的对象是int *,这就是你的循环变量需要的东西。

for (int* e : a) ...

Or

for (int*& e : a) ...

Or simply:

for (auto& e : a) ...