在使用cin ..阅读输入之前刷新cout为什么? [英] Flushing of cout prior to reading input using cin .. why?

查看:149
本文介绍了在使用cin ..阅读输入之前刷新cout为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么cout在cin开始阅读之前必须刷新?他们不是不同的缓冲区吗?我可以读取输入到缓冲区,同时把它放在输出缓冲区(刷新前).. 2个不同的缓冲区。我在这里感到困惑。

Why does cout has to be flushed before cin starts reading? Aren't they of different buffer? I can have reading of input into a buffer while same time putting it on output buffer (before flushing) .. 2 different buffers. I am confused here.

推荐答案

它不必刷新。默认情况下,流绑定在一起,所以当你做的事情:

It doesn't have to be flushed. By default the streams are tied together so that when you do things like:

cout << "Enter your name:";
cin >> name;

在输入开始之前出现提示 - 这只是一个方便的功能。但是,您可以解除他们:

the prompt appears before the input starts - this is just a convenience feature. However, you can untie them:

cin.tie( static_cast<ostream*>(0) );

之后cout将不会被flush(在输入cin之前)。

following which cout will not (necessarily) be flushed before input is performed on cin.

这篇关于在使用cin ..阅读输入之前刷新cout为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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