简体   繁体   English

增量++ i,i ++和i + = 1

[英]Increment ++i, i++ and i+=1

I am beginner in C++. 我是C ++的初学者。 What I understand is that:- 我的理解是: -

i++ is executing first, then increment, ++i is increment first, then execute,i+=1 is increment by 1,then execute. i ++首先执行,然后递增,++ i先递增,然后执行,i + = 1递增1,然后执行。 But in the FOR loop: 但是在FOR循环中:

for (i=0;i<10;i++)
for (i=0;i<10;++i)

There is really no difference in these two loops above. 上面这两个循环确实没有区别。

Here is another one to calculate the sum of all integers from 1 to 100: 这是另一个计算从1到100的所有整数的总和:

int i=1, sum=0;
while (i<=100)
{
    sum+=i;
    i++;         //i+=1;    ++i;
}
cout<<sum<<" "<<i<<endl;
return 0;

But if I replace i++ with i+=1 or ++i, they all return a sum of 5050 and i of 101. So I really don't see any difference in them. 但是如果我用i + = 1或++ i替换i ++,它们都会返回5050和101的总和。所以我真的看不出它们有什么不同。

So could anyone explain this to me? 那么有人可以向我解释这个吗? Which one of those is used most in programming? 编程中哪一个最常用? Thank you!! 谢谢!!

You are correct. 你是对的。 In your examples there is no difference. 在您的示例中没有区别。

But here there is: 但这里有:

int i = 0;
cout << i++ << endl;  //prints 0
cout << i   << endl;  //prints 1

vs VS

int i = 0;
cout << ++i << endl;  //prints 1
cout <<   i << endl;  //prints 1

Which one of those is used most in programming? 编程中哪一个最常用?

Most of the time, ++ is the only operation in the statement (FYI, a for loop has three statements). 大多数情况下, ++是语句中唯一的操作(FYI, for循环有三个语句)。

If it isn't, then it might matter, and you'll use whichever one gives you the correct behavior. 如果不是,那么它可能很重要,你将使用任何一个给你正确的行为。

FYI FYI

Some developers have the opinion that if pre and postfix operators should always be used alone (not part of a large statement). 一些开发人员认为,如果pre和postfix运算符应该总是单独使用(不是大语句的一部分)。 They can lead to confusing code, or even undefined behavior. 它们可能导致令人困惑的代码,甚至是未定义的行为。

For example, 例如,

int i = 0;
cout << i++ + i++ << endl;

has undefined behavior. 有未定义的行为。

So could anyone explain this to me? 那么有人可以向我解释这个吗?

What i++ does is return the current value of i and then increment it by one, and ++i first increment i by 1 and then returns the value of i . 什么i++的作用是返回的当前值i ,然后由一个增加它,并++i第一次增加i1 ,然后返回的值i

Take a look at this, for example: 看看这个,例如:

i = 5;
j = 5;
res = i++;    //res = 5, but i=6
res = ++j;    //res = 6 and j=6

Which one of those is used most in programming? 编程中哪一个最常用?

Both are used depending on what you want or may be how you want. 两者都可以根据您的需要使用,也可以根据您的需要使用。


One more thing to note: ++i is an l-value, but i++ is not. 还有一点需要注意: ++i是l值,但i++不是。 For details see here 详情请见此处

Your analysis is correct. 你的分析是正确的。 i++ will return the value of i , then increment, whereas ++i will increment the value of i , then return the new value. i++返回的值i ,然后增加,而++i将递增的价值i ,然后返回新值。 i += 1 will do the same as ++i . i += 1将与++i相同。 The difference in where they will be used in actual code is primarily situational; 它们在实际代码中的使用位置差异主要是情境; there's no specific answer as to where each of them are most often used or helpful. 关于每个人最常使用或有帮助的地方,没有具体的答案。 It all depends on what you want to do. 这一切都取决于你想做什么。

Here's a contrived example of one time it might be useful to use post-increment ( i++ form): 这是一个人为的例子,一次使用后增量( i++形式)可能有用:

// Return the length of a string, including the null byte
int stringLength(char str[])
{
    int length = 0;
    while (str[length++] != 0);
    return length;
}

If you wanted to return the length without the null byte, you could modify the above example slightly, using the ++i form: 如果要返回没有空字节的长度,可以使用++i表单稍微修改上面的示例:

// Return the length of a string, without the null byte
int stringLength(char str[])
{
    int length = -1;
    while (str[++length] != 0);
    return length;
}

As for i += 1 , I don't think I've ever done quite that, since you can use pre- or post-increment instead. 至于i += 1 ,我认为我没有做过那么多,因为你可以使用预增量或后增量。 I've generally only used the compound assignment operators for values other than 1. 我通常只将复合赋值运算符用于1以外的值。

I think if you imagine how the for loop works you can understand the problem at hand. 我想如果您想象for循环如何工作,您可以理解手头的问题。

for loop for循环

initialization    --> i = 0

<check condition>  --> i<10? ------->--------------------
 |                      |                                |
 ^                      |yes(i.e condition not met)      |no(i.e condition met)
 |                      V                                V
 |                  --------------
 |                  |body of loop|
 |                  --------------
 |                      |
 |                      V
 -------<------------increment i (**)                exit for loop

** increment i means i++ or ++i ** increment i表示i++++i

i++ can be replaced by this : i++可以替换为:

int temp = i;
i = i + 1;

temp will be useless here so the compiler will optimize it to just inc i instruction. temp在这里没用,所以编译器会将它优化为inc i指令。 even if it doesn't do that temp is just a waste of space that's all. 即使它没有那么temp也只是浪费空间而已。

++i can be replaced by ++我可以被替换

i = i + 1;
int temp = i; 

again temp is not required so the compiler will just replace it with inc i instruction. 再次temp不是必需的,所以编译器只需用inc i指令替换它。

if you see both the instruction are the same because they are not being assigned to anything. 如果你看到两条指令都是相同的,因为它们没有被分配给任何东西。 only i is being affected by the increment. 只有i受到增量的影响。 so both are essentially the same. 所以两者基本相同。 this is true if i is a built-in type . 如果i是内置类型,这是真的。

you see that the increment instruction is placed after the body of the loop? 你看到增量指令放在循环体之后? can you now see that this is almost similar to the while loop that you showed? 你现在能看到这几乎与你展示的while循环相似吗?

it is always nice to think of loops in this way. 以这种方式思考循环总是很好的。

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

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