简体   繁体   English

我的range-for循环出现逻辑错误

[英]Logic error with my range-for loop

I'm studying C++ as a beginner (I started 2 months ago) and I have a problem with my simple code. 我正在学习C ++作为初学者(我在2个月前开始)并且我的简单代码存在问题。 I tried to set the value of each element in this vector to 0 but I can't understand why it doesn't work : 我试图将此向量中每个元素的值设置为0但我无法理解为什么它不起作用:

vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 

for (int x : numbers) x = 0; 

I know that I may sound stupid but I am a beginner. 我知道我可能听起来很愚蠢,但我是初学者。 If I try to do the same thing with a traditionally for loop it works, why ? 如果我尝试使用传统的for循环来做同样的事情,那么为什么呢?

for (int& x : numbers) x = 0; 

为了改变容器元素,必须使用范围循环迭代变量的引用(默认情况下,迭代变量是当前迭代元素的副本

It does not change the values in the array, because in each iteration the value in the array is assigned to to x and you are changing x and not the value in the array. 它不会更改数组中的值,因为在每次迭代中,数组中的值都分配给x,而您正在更改x而不是数组中的值。

Basically the range based loop is similar to the following ordinary for loop: 基本上,基于范围的循环类似于以下普通for循环:

for(int i = 0; i < numbers.length(); i++)
{
     int x = numbers[i];

     //your code
}

.

For more information check out the c++ documentation: http://en.cppreference.com/w/cpp/language/range-for . 有关更多信息,请查看c ++文档: http//en.cppreference.com/w/cpp/language/range-for It states: 它指出:

range_expression is evaluated to determine the sequence or range to iterate. 计算range_expression以确定要迭代的序列或范围。 Each element of the sequence, in turn, is dereferenced and assigned to the variable with the type and name given in range_declaration. 反过来,序列的每个元素都被解除引用并分配给变量,其范围和名称在range_declaration中给出。

Also you will find there the example "Shmil The Cat" has posted and more examples which will help you understand how the range loop works. 你也会在那里找到“Shmil The Cat”的例子和更多的例子,它们将帮助你理解范围循环的工作原理。

It does not work, you don't have any reference to the elements in the array via your foreach . 它不起作用,你没有通过foreach对数组中的元素进行任何引用。 Think about this, you are retrieving elements in array numbers and making a copy of copy in x which does not have reference. 想一想,你在检索数组元素的numbers ,使副本的副本, x不具有参考。 so any changes that x undergoes does not reflect in numbers arrays. 所以x经历的任何变化都不会反映在数字数组中。

so work how @Shmil The Cat suggested. 那么工作@Shmil The Cat如何建议的。 or traditional way will also do the job like you said. 或传统方式也会像你说的那样完成工作。

The items of vector are not assigned the value (zero in this case)because ranged based loops consider container as copy of original until explicitly passed the reference of original container. 向量项未赋值(在本例中为零),因为基于范围的循环将容器视为原始副本,直到显式传递原始容器的引用。

Do use 好用

for (int& x : numbers) x = 0; 

as suggested by @Shmil The Cat 正如@Shmil The Cat所建议的那样

To help you understand, here is how you can read the below: 为了帮助您理解,以下是您可以阅读以下内容的方法:

For every element in numbers , copy data into x and set x to 0. // Now this doesn't touch the value in the vector. 数字的每一个元素, 数据复制x和设置x到0 //现在此不接触在向量中的值。

for (int x : numbers) 
    x = 0; 

Reading the below loop: 阅读以下循环:
For every element in numbers , make a reference to it, and change the value through the reference to 0. This on the other hand, changes the values in the vector. 对于数字中的每个元素,对其进行引用 ,并通过引用将值更改为0.另一方面,更改向量中的值。

for (int &x : numbers)
    x = 0;

如果您只想为所有元素设置特定值,最好使用std::fill

std::fill(numbers.begin(), numbers.end(), 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM