首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

cout的输出顺序?该怎么解决

2012-02-16 
cout的输出顺序?#includeiostreamusingnamespacestdintswaptest(int&a,int&b){inttempaabbtempre

cout的输出顺序?
#include   <iostream>
using   namespace   std;
int   swaptest(int   &   a,   int   &   b)
{
int   temp   =   a;
a   =   b;
b   =   temp;
return   0;   //   no   use
}

int   main()
{
//   output   in   the   same   line
int   a=6,   b=7;
cout   < <   swaptest(a,b)   < <   '\\t '   < <   a   < < '\\t '   < <   b   < <endl;

//again   assignment
a   =   6;
b   =   7;
//   output   in   two   line
cout   < <   swaptest(a,b)   < <   '\\t ';
cout   < <   a   < < '\\t '   < <   b   < <endl;

return   0;
}
The   result   is:
0         6         7
0         7         6
WHY??



[解决办法]
是参数求值顺序问题。
如果你期望确定的顺序,就不要写cout < < swaptest(a,b) < < '\\t ' < < a < < '\\t ' < < b < <endl;这样依赖求值顺序的代码。
[解决办法]


vc 求顺序从右至左
[解决办法]
< <和> > 输入输出符号其实是一个输入输出函数重载过后的东西,为了方便使用写成了这样的形式

后面跟着的输出内容其实都是函数的参数,对于不同的编译器,函数参数的求值顺序可能有所不同,在vc中是从右至左.
[解决办法]
vc中是从右至左
你那样写太不规范了~~~~~~

热点排行