c++缓冲区------c++ Primer Plus
通常,通过使用缓冲区可以更高效地处理输入和输出。缓冲区是用作中介的内存块,它是将信息从设备传输到程序或从程序传输给设备的临时存储工具。通常,像硬盘驱动器这样的设备以512字节(或更多)的块为单位来传输信息,而程序通常每次只能处理一个字节的信息。缓冲区帮助匹配这两种不同的信息传输速率。例如,假设程序要计算记录在硬盘文件中的金额。程序可以从文件中读取一个字符,处理它,再从文件中读取下一个字符,再处理,以此类推。从硬盘文件冲每次读取一个字符需要大量的硬件活动,速度非常慢。缓冲方法则从硬盘上读取大量信息,将这些信息存储在缓冲区中,然后每次从缓冲区里读取一个字节。输出时,程序首先填满缓冲区,然后把整块数据传输的给硬盘,并清空缓冲区,以备下一批输出使用。这被称为刷新缓冲区。
键盘输入每次提供一个字符,因此这中情况下,程序无需缓冲区来帮助匹配不同的数据传输速率。然而,对键盘输入进行缓冲可以让用户在将输入传输给程序之前返回并更正。c++程序通常在用户按下回车时刷新输入缓冲区。对于屏幕输出,c++程序通常在用户发送换行符时刷新缓冲区。程序也可能会在其他情况下刷新输入
由于ostream类对cout对象出的的输出进行缓冲,所以输出不会立即发送到目标地址,而是被存储在缓冲区中,直到缓冲区填满。然后,程序将刷新缓冲区,把内容发送出去,并清空缓冲区,以存储新的数据。
但不一定。在屏幕输出时,程序不必等到缓冲区被填满。例如,将换行符发送到缓冲区后,将刷新缓冲区。另外,多数c++实现都会在输入即将发生时刷新缓冲区。例如
-
cout<<
"aaaaa";
-
int n;
-
cin>>n;
程序期待输入这一事实,将导致它立刻显示cout信息(即“aaaaa”),即使输出字符串中没有换行符。如果没有这一特性,程序将等待输入,而无法通过cout信息来提示用户。
还可以使用两个控制符来强行进行刷新flush 和 endl
cout<<"asd"<<flush; //刷新缓冲区
flush(out); //刷新缓冲区
cout<<""sdf"<<endl; //刷新缓冲区并插入一个换行符
一般来说,有以下几种操作会刷新缓冲区:
- cout被析构
- 缓冲区满
- 使用操纵符主动刷新,例如endl,fflush
- 使用unitbuf操纵符设置流的内部状态,使得每次向流中插入字符都会刷新流
- 使用cin读取字符串
但当测试以下代码时:
-
#include<iostream>
-
using
namespace
std;
-
int main()
-
{
-
cout<<
"aaa";
-
while(
1);
-
return
0;
-
}
虽然不满足上述操作中的任何一个,但字符串aaa仍被输出。vs,g++上均是如此。
经查找资料了解到:当系统空闲时(还有种说法是长时间无输入时)缓冲区会自动刷新。当然这种刷新只是一种“请求”,并不能保证刷新,会不会刷新完全看系统心情
所以,并非未刷新缓冲区就输出了字符串,而是系统比较闲,所以顺手帮你刷新了缓冲区。但要想保证在该输出的地方输出,还是安安心心使用endl或flush吧
http://www.cnblogs.com/cknightx/p/6992417.html
c++ Primer Plus
</div>
<div class="postDesc">posted @ <span id="post-date">2017-10-18 23:18</span> <a href="https://www.cnblogs.com/l2017/" rel="nofollow">ff_d</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=7689789" rel="nofollow">编辑</a> <a href="#" rel="nofollow" target="_self">收藏</a></div>
</div> </div>
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/li_haoren">
<img src="https://profile.csdnimg.cn/8/0/8/3_li_haoren" class="avatar_pic" username="li_haoren">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/li_haoren" data-report-click="{"mod":"popu_379"}" target="_blank">li_haoren</a></span>
</div>
<div class="text"><span>发布了127 篇原创文章</span> · <span>获赞 16</span> · <span>访问量 1万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=li_haoren" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
</article>