About the order of execution of successive i++ in cout

Source: Internet
Author: User

[C + +] learning about i++ and ++i

Moakap today in the forum inadvertently see someone to ask about i++ and ++i problem, feel oneself also not familiar with, took a closer look. Feel a hero's reply is quite good, very characteristic, put the question very clear, teachable. Here is an excerpt, learning to learn. --------------------------------------------------------------------------------------------------------about + + The difference between I and i++, if described by a function, is equivalent to the following two functions:
++i:C + + codeint PPI (int & i) {i = i + 1; return i; } i++:C + + codeint IPP (int & i) {int t = i;      i = i + 1; return t; }
It is obvious that two functions add the value of I by 1, but the value returned is not the same. Besides, IPP is more than PPI.C + + codeint t = i;
This step, so ++i efficiency is a little higher than i++.
The same is the same with i--.

a second question: The difference between ++i and i++ when cout and printf multiple outputs. It's about the difference between the cout<<i<< "<<++i<<endl; and cout<<i<<" <<i++<<endl; cout< <i<< "" <<++i<<endl; and cout<<i<< ""; The difference between the cout<<++i<<endl;
Here's an example to illustrate this problem:C + + code#include <stdio.h> #include <iostream.h> int main (void) {int i = 0; cout << "Test Execute Order:" << Endl << cout: "<< i <<", "<& Lt       ++i << Endl;     i = 0;        printf ("printf ():%d,%d/n", I, ++i); return 0; }
The results of this code under VC6.0 and BC3.1 are:
Test Execute Order:
Cout:1, 1
printf (): 1, 1

Cout < < is actually executing the overloaded operator (< <) function of the cout object:C + + codeostream& operator << (datatype);
The datatype here are only the corresponding data types. The invocation of this function can be written as cout < < "a" < < 3 < < "B" < < Endl; this concatenation of the output form is because it returns cout this Ostream object Body, and the returned Cout object continues to invoke the above function to output the next operand, so repeatedly until the statement is finished (and, of course, the command to flush the buffer will have to be refreshed first). As a cout, the order of execution is left to right.

As for why the value of I was first changed. That's because the C + + standard does not dictate the order in which expressions are solved, and it is up to the compiler to decide. As a result, the implementations of each compiler are somewhat different. For example, if the above routine is executed under GCC (g++), the result is:
Test Execute Order:
cout:0, 1
printf (): 1, 1

So, VC, BC and GCC (g++) to this kind of concatenation form of expression solution order is different, the latter is from left to right, the former is to keep the order of the function, that is, from right to left. Both are in the same order of solving the expression of the function arguments, that is, the output from right to left (see printf ()).

So.   The programming advice does not recommend that the same variable be modified inside the same expression. On platform: sparc-sun-solaris2.10-g++ (GCC) 4.0.3 (GCCFSS), the result is:
Test Execute Order:
cout:0, 1
printf (): 1, 1

On the platform: g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3), the result is:
Test Execute Order:
Cout:1, 1
printf (): 1, 1

Again, this confirms the correctness of the conclusion that the "expression evaluation order is undefined". To sum up:about i++ and ++i: 1. Both pair I plus 1, but the order of processing is different. i++ first to the related operation, and then add 1. The ++i is incremented first, then the related operation. 2. When used alone, ++i efficiency is higher than i++ efficiency. about cout<<i<<i++<<endl; and cout<<i<<++i<<endl; are: 1. cout is an overload of the << operator, which returns the cout the Ostream object itself. << continuous use, the returned Cout object continues to call the above function to output the next operand, so repeated until the statement is finished. Therefore, the cout execution order is left to right. 2. C + + standard for the expression of the solution order is not specified, according to the compiler different. Therefore,,cout<<i<<++i<<endl; output in different compiler environments is not always performed in the order of cout left to right. 3. The expression processing order of the compiler function argument is right-to-left.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.