首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

C++基础解二十四

2008-12-06 
COUT输出顺序

    代码1:
  #include <iostream> //程序片段1
  using namespace std;
  int f(int a)
  {
  auto int b=0; //auto可以省略
  static int c=3;
  b+=1;
  c+=1;
  return a+b+c;
  }
  int main()
  {
  int a=2;
  for(int i=0;i<3;i++)
  cout<<f(a)<<" ";
  cout<<f(a)<<" "<<f(a)<<" "<<f(a)<<endl;
  return 0;
  }
  结果:7 8 9
  代码2:
  #include <iostream>//程序片段2
  using namespace std;
  int f(int a)
  {
  auto int b=0; //auto可以省略
  static int c=3;
  b+=1;
  c+=1;
  return a+b+c;
  }
  int main()
  {
  int a=2;
  cout<<f(a)<<" "<<f(a)<<" "<<f(a)<<endl;
  return 0;
  }
  结果:9 8 7
  解析:
  cout的输出是先从右往左读入缓冲区,再从左往右输出;
  假设a = 1; b = 2; c = 3;
  cout<<a<<b<<c<<endl;
  缓冲区:|3|2|1|<- (把“<-”看作是指针)
  输出:|3|2|<- (输出 1)
  |3|<- (输出 2)
  |<- (输出 3)
  你的f(a)是直接就是在缓冲区的:|7|8|9|,所以cout直接从缓冲区读出数据
  输出<-
  缓冲区相当于堆栈的效果。。。

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行