C++ 大多数人将 cin::sync() 视为清除缓存区函数的误用

 

ps:我发现有网站将我之前写的标题为:C++ 关于大多数人将cin::sync()视为清楚缓冲区函数的错误   的文章转载了,声明一下那篇文章中的内容可能存在错误,本人已删,请注意。

 

一百度,大多数人将cin::sync()函数直接理解为清空缓冲区的函数,然而如果在VS2017编译器上,就会发现并不能清空缓冲区,为什么呢?

 

http://en.cppreference.com/w/cpp/io/basic_istream/sync

 

根据此标准文档中的说明:

该函数的作用为:Synchronizes the input buffer with the associated data source.

 

然后重点:

As with readsome(), it is implementation-defined whether this function does anything with library-supplied streams. The intent is typically for the next read operation to pick up any changes that may have been made to the associated input sequence after the stream buffer last filled its get area. To achieve that, sync() may empty the get area, or it may refill it, or it may do nothing. A notable exception is Visual Studio, where this operation discards the unprocessed input when called with a standard input stream.

 

此函数是: implementation-defined

 


所以实现上,可不一定能清空缓冲区,具体实现跟编译器相关,所以不具备很好的移植性,慎用!

 

具体情况可参考该网站的具体说明!

转载于:https://www.cnblogs.com/Champion-L/p/7205080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值