首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于cin.unget()和cin.putback()的有关问题

2012-02-15 
关于cin.unget()和cin.putback()的问题cin.unget()和cin.putback()都是将字符放回输入流中,那么他们两者之

关于cin.unget()和cin.putback()的问题
cin.unget()和cin.putback()都是将字符放回输入流中,那么他们两者之间有什么区别吗?

[解决办法]
功能上真的好像没有差别, 实现上有点不同。。。

///////////////////////////
istream& unget ( );
Decrement get pointer
Decrements the internal get pointer by one, making the last character extracted from the input sequence available again as the next character to be read from the stream.

The function effectively calls the sungetc member function of the streambuf object associated to the stream.
A subsequent call to member gcount will return zero.

////////////////////////////
istream& putback ( char c );
Put character back
Decrements the internal get pointer by one, and c becomes the character to be read at that position by the next input operation.

The function effectively calls the sputbackc member function of the streambuf object associated to the stream.
A subsequent call to member gcount will return zero.
[解决办法]
功能是一样的,最大的区别在参数上
unget没有参数,是把已经从流读取出来的那个字符放回去,下次读取的时候可以读到这个字符
而putback是把参数c放入流中,下次读取就能读到c了

热点排行