[英]Regarding i++ and ++i in c++
There are following two programs for finding largest and secondlargest integers: 有以下两个程序用于查找最大和第二大整数:
1. 1。
int main()
{
int const no_of_elements =10;
int list[no_of_elements] = { 1, 2, 3, 4, 5, 10, 9, 6, 7, 10 };
int largest = list[0];
int second_largest = list[0];
int pos_largest = 0;
int pos_second_largest = 0;
int i;
for (i = 0; i < no_of_elements; i++)
{
if (largest <= list[i])
{
second_largest = largest;
largest = list[i];
pos_second_largest = pos_largest;
pos_largest = i;
}
}
cout << "Largest number is : " << largest << "\n";
cout << "And it is at the position : " << ++pos_largest << endl;
cout << "Second largest number is : " << second_largest << " and its at the position : " << ++pos_second_largest << endl;
return 0;
}
2. In for loop, i do pos_largest = ++i
2.在for循环中,我执行
pos_largest = ++i
They give different answers: 他们给出不同的答案:
1. gives largest=10 secondlargest=10
ie as required 1.给出
largest=10 secondlargest=10
即根据需要
but 2. gives largest=10 secondlargest=9
但是2.给出
largest=10 secondlargest=9
Kindly explain???? 请解释一下????
i++
is post-increment. i++
是后递增的。 This means "give me the value of i
, but after that, increment it". 这意味着“给我
i
的值,但在那之后增加它”。
++i
is pre-increment. ++i
是预递增的。 This means "increment i
, then give me its new value". 这意味着“增加
i
,然后给我它的新价值”。
int i = 1;
int a = i++; // a is now 1, i is 2
i = 1; // reset i's value to 1
int b = ++i; // i's value is incremented to 2, b's value is 2
Regarding the for
loops, both of these have the same effect 关于
for
循环,这两者具有相同的效果
int i;
for (i = 0; i < 10; i++) {
// ...
}
for (i = 0; i < 10; ++i) {
// ...
}
The third section in the for loop is run after each iteration (execution) of the loop body. for循环中的第三部分在循环主体的每次迭代(执行) 之后运行。
In your #2, when you hit the first 10
in your list, you assign pos_largest
to be 6 (ie,. where 9 is) instead of 5 (where the 10 is) because you've pre-incremented the i
before the assignment. 在#2中,当您点击列表中的前
10
时,您将pos_largest
分配为6(即9在哪里)而不是5(其中10在哪里),因为在分配之前您已将i
预先增加了。 Thus, later when you hit the second 10
in your list, it becomes the largest and your second largest is now set to 9 (ie, the value in spot 6). 因此,稍后您在列表中点击第二个
10
时,它将变为最大,而第二个最大现在被设置为9(即,点6中的值)。
++i
and i++
is i+=1
. ++i
和i++
是i+=1
。 The former will increase i
first then does operation, the latter does the operation with i
, after that increase its value by 1
. 前者先增加
i
然后进行运算,后者先对i
进行运算,之后将其值加1
。
Well your main problem is to understand the difference between ++i (preincrement) and i++ (postincrement). 好吧,您的主要问题是了解++ i(预增量)和i ++(后增量)之间的区别。 ++i means that it quickly adds the value 1 in the next coming variable or lvalue.
++ i表示它将在下一个即将到来的变量或左值中快速添加值1。 And the meaning of the i++ is that it will not add 1 to the previous lvalue or variable untill a sequence point comes.
i ++的含义是在序列点到来之前,它不会对先前的左值或变量加1。 And remember that sequence points are ;
并记住顺序点是; { } && ||
{} && || etc. e.
等e。 if you have a code like this.
如果您有这样的代码。
int test=6;
cout<<(test++)<<endl;
cout<<test<<endl;
You will be thinking that it will print like this 您会以为它将像这样打印
7
7
But it will not because it is a post increment. 但这不是因为这是职位增加。 It will not add 1 to test untill a sequence point comes and the next sequence point is ;
在一个序列点出现并且下一个序列点是之前,它不会加1来测试; .
。 so it will print like this.
因此它将像这样打印。
6
7
Opposite is the case for preincrement. 与之相反的是预增量。 It will quickly add 1 to the variable (ie lvalue otherwise it will give error ) and then display 7 without waiting for the sequence point.
它将快速将1加到变量中(即,左值,否则将给出error),然后显示7,而无需等待序列点。
If you use i++ it completes the operation before incrementing the variable. 如果使用i ++,它将在增加变量之前完成操作。 If you use ++i it increments before completing the operation.
如果使用++ i,它将在完成操作之前递增。 In most cases you want to use i++, though it greatly depends on how you're using it.
在大多数情况下,您都想使用i ++,尽管它在很大程度上取决于您的使用方式。 For example:
例如:
for(int i = 0; i < 5; i++){
cout << "i = "<< i << endl;
}
This will complete the operation, then increment. 这将完成操作,然后递增。 However, if you use this:
但是,如果使用此命令:
for(int i = 0; i < 5; ++i){
cout << "i = " << i << endl;
}
it will increment first before printing the value 在打印值之前它将先增加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.