cout的缓冲区如何工作? [英] How the buffer of cout work?

查看:190
本文介绍了cout的缓冲区如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道cout前几天有缓冲区,当我用google搜索它时,据说缓冲区有点像堆栈,并从右到左获取cout和printf的输出,然后将它们放到(控制台或文件)。这样,

I know that cout have buffer several days ago, and when I google it, it is said that the buffer is some like a stack and get the output of cout and printf from right to left, then put them out(to the console or file)from top to bottem. Like this,

a = 1; b = 2; c = 3;
cout<<a<<b<<c<<endl;
buffer:|3|2|1|<-   (take "<-" as a poniter)

output:|3|2|<-     (output 1)
        |3|<-       (output 2)
        |<-         (output 3)

然后我在下面编写代码,

Then I write a code below,

#include <iostream> 
using namespace std; 
int c = 6;
int f() 
{   
    c+=1; 
    return c; 
} 

int main() 
{ 
     int i = 0; 
     cout <<"i="<<i<<" i++="<<i++<<" i--="<<i--<<endl; 
     i = 0;
     printf("i=%d i++=%d i--=%d\n" , i , i++ ,i-- );

     cout<<f()<<" "<<f()<<" "<<f()<<endl; 
     c = 6;
     printf("%d %d %d\n" , f() , f() ,f() );
     system("pause");
     return 0; 
}

在VS2005下,输出为

Under VS2005, the output is

i=0 i++=-1 i--=0
i=0 i++=-1 i--=0
9 8 7
9 8 7

看来堆栈方式是正确的〜
但是,我昨天读了C ++ Primer Plus,据说cout从左到右执行,每次返回一个对象(cout),因此这是允许您使用插入来连接输出的功能。但是从左到右的方式不能解释cout<

然后 Alnitak 告诉我,<<运算符实际上是ostream&运算符<(ostream& os,int),因此另一种写法是:
运算符<(运算符<(运算符<(cout,a),b),c ),

It seems that the stack way is right~ However, I read C++ Primer Plus yesterday, and it is said that the cout work from left to right, every time return an object(cout), so "That’s the feature that lets you concatenate output by using insertion". But the from left to right way can not explain cout<

Then Alnitak tell me that, "The << operator is really ostream& operator<<(ostream& os, int), so another way of writing this is: operator<< ( operator<< ( operator<< ( cout, a ), b ), c )",

如果首先评估了最正确的参数,则可以解释一下。

If the rightest argument is first evaluated, it can be some explained.

现在我我对cout的缓冲区的工作方式感到困惑,有人可以帮我吗?

Now I'm confused about how cout's buffer work, can somebody help me?

推荐答案

您正在混合很多东西。迄今为止:

You are mixing a lot of things. To date:


  • cout

  • 的实施细节
  • 被限制的呼叫

  • 呼叫约定

尝试分别阅读它们。


printf( i =%d i ++ =%d i-= %d\n,i,i ++,i--);

printf("i=%d i++=%d i--=%d\n" , i , i++ ,i-- );

以上行引用了未定义的行为。阅读常见问题解答 3.2 。请注意,您观察到的是函数调用约定的副作用,以及特定实现(即您的实现)在堆栈中传递参数的方式。如果您在其他计算机上工作,则不能保证这是相同的。

The above line invokes undefined behavior. Read the FAQ 3.2. Note, what you observe is a side-effect of the function's calling convention and the way parameters are passed in the stack by a particular implementation (i.e. yours). This is not guaranteed to be the same if you were working on other machines.

我认为您将函数调用的顺序与缓冲混淆了。当您有一个 cout 语句后跟多次插入<< 时,实际上是在调用多个函数,另一个。因此,如果您要编写:

I think you are confusing the order of function calls with buffering. When you have a cout statement followed by multiple insertions << you are actually invoking multiple function calls, one after the other. So, if you were to write:

cout << 42 << 0;

这实际上意味着:您打电话,

It really means: You call,

cout = operator<<(cout, 42)

然后在另一个调用相同运算符的调用中使用return:

and then use the return in another call to the same operator as:

cout = operator<<(cout, 0)

以上所测试的内容不会告诉您 cout 的内部表示。我建议您看一下头文件以了解更多信息。

What you have tested by the above will not tell you anything cout's internal representation. I suggest you take a look at the header files to know more.

这篇关于cout的缓冲区如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆