int i =10;
cout  << " " << i++<< " " << ++i <<  "  " << i++ << "  " << ++i << endl;我输出结果是: 13 13 11 11 
谁能解释一下

解决方案 »

  1.   

    from right to left:
    1、++i  i is 11 and output is 11
    2、i++  output i = 11 then i++ ,i = 12
    3、++i  i is 13 and output is 13
    4、i++  output i = 13 then i++ ,i = 14
      

  2.   

    优先顺序是这样的:"++i" > "<<" > "i++"
    所以执行顺序如下:  
    从右到左,但是"i++"要在"<<"结束以后才进行+1操作(1) i = i + 1;
    (2) <<i;
    (3) <<i;
    (4) i = i+1;
    (5) <<i;
    (6) <<i;
    (7) i = i+1;
    (8) i = i+1;
      

  3.   

    from left to right:
    1、i++;  //  i is 10,  print "10",  then i=i+1,  i is 11;
    2、++i;  //  i is 11,  i=i+1,       i is 12,     then print"12";
    3、i++;  //  i is 12,  print "12",  then i=i+1,  i is "13" 
    4、++i;  //  i is 13,  i=i+1,       i is "14",   then print"14"//complied by GCC
      

  4.   

    The result only depend on the compiler.
      

  5.   

    The result not only depend on the compiler ,but optimization.
      

  6.   

    如果不优化,结果为
    13 13 11 11 
    汇编如下:
    _main PROC NEAR
    ; File t.cpp
    ; Line 5
    push ebp
    mov ebp, esp
    sub esp, 12 ; 0000000cH
    ; Line 6
    mov DWORD PTR _i$[ebp], 10 ; 0000000aH
    ; Line 7
    push OFFSET FLAT:?endl@@YAAAVostream@@AAV1@@Z ; endl
    mov eax, DWORD PTR _i$[ebp]
    add eax, 1
    mov DWORD PTR _i$[ebp], eax
    mov ecx, DWORD PTR _i$[ebp]
    push ecx
    push OFFSET FLAT:$SG1342
    mov edx, DWORD PTR _i$[ebp]
    mov DWORD PTR -8+[ebp], edx
    mov eax, DWORD PTR -8+[ebp]
    push eax
    mov ecx, DWORD PTR _i$[ebp]
    add ecx, 1
    mov DWORD PTR _i$[ebp], ecx
    push OFFSET FLAT:$SG1341
    mov edx, DWORD PTR _i$[ebp]
    add edx, 1
    mov DWORD PTR _i$[ebp], edx
    mov eax, DWORD PTR _i$[ebp]
    push eax
    push OFFSET FLAT:$SG1340
    mov ecx, DWORD PTR _i$[ebp]
    mov DWORD PTR -12+[ebp], ecx
    mov edx, DWORD PTR -12+[ebp]
    push edx
    mov eax, DWORD PTR _i$[ebp]
    add eax, 1
    mov DWORD PTR _i$[ebp], eax
    push OFFSET FLAT:$SG1339
    mov ecx, OFFSET FLAT:?cout@@3Vostream_withassign@@A
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z ; ostream::operator<<
    ; Line 8
    mov esp, ebp
    pop ebp
    ret 0
    _main ENDP
    _TEXT ENDS
    ; COMDAT ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z
    _TEXT SEGMENT
    ---------------------------------------------------------------------------------
    优化编译,结果为
    13 14  11  14
    汇编如下:
    _main PROC NEAR ; COMDAT
    ; File t.cpp
    ; Line 7
    mov eax, OFFSET FLAT:??_C@_02IHLC@?5?5?$AA@ ; `string'
    push 14 ; 0000000eH
    push eax
    push 11 ; 0000000bH
    push eax
    mov eax, OFFSET FLAT:??_C@_01FCOA@?5?$AA@ ; `string'
    push 14 ; 0000000eH
    push eax
    push 13 ; 0000000dH
    push eax
    mov ecx, OFFSET FLAT:?cout@@3Vostream_withassign@@A
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@PBD@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@H@Z ; ostream::operator<<
    push OFFSET FLAT:?flush@@YAAAVostream@@AAV1@@Z ; flush
    push 10 ; 0000000aH
    mov ecx, eax
    call ??6ostream@@QAEAAV0@E@Z ; ostream::operator<<
    mov ecx, eax
    call ??6ostream@@QAEAAV0@P6AAAV0@AAV0@@Z@Z ; ostream::operator<<
    ; Line 8
    ret 0
    _main ENDP
    _TEXT ENDS
    --------------------------------------------------------------------------------------
    这种问题在C/C++版很多,有时与编译器有关,所以在编程时应尽量避免
      

  7.   

    楼主的结果原因很简单,输出是从右到左运行(计算)的。在C语言里也是这样。。不信你可以用 printf()试试。
      

  8.   

    是人一般不会这么写,不要钻牛角尖,很感兴趣可以分析汇编代码,MS和Borland的编译器对这个问题的处理有明显区别