在线文档教程
C++
输入/输出 | Input/output

std::basic_istream::putback

性病:基本[医]iStream::putback

basic_istream& putback( char_type ch

把字符ch返回到输入流,因此下一个提取的字符将是ch...

先清eofbit,则表现为UnformattedInputFunction在构造和检查哨兵对象之后,如果rdbuf()不为空,调用rdbuf()->sputbackc(ch),这就叫rdbuf()->pbackfail(ch)如果ch不等于最近提取的字符。

如果rdbuf()为空或如果rdbuf->sputbackc(ch)回报Traits::eof(),电话setstate(badbit)...

在任何情况下,设置gcount()对零。

参数

ch-character to put into input stream

返回值

*this...

例外

failure如果发生错误%28,则错误状态标志不是goodbit29%和exceptions()将被抛向那个州。

如果内部操作抛出异常,则会捕获该操作,并且badbit已经设定好了。如果exceptions()设置为badbit,异常将被重新抛出。

演示修改和不修改的腐返回%28%29之间的区别.

二次

#include <sstream> #include <iostream> int main() { std::stringstream s1("Hello, world" // IO stream s1.get( if (s1.putback('Y')) // modifies the buffer std::cout << s1.rdbuf() << '\n'; else std::cout << "putback failed\n"; std::istringstream s2("Hello, world" // input-only stream s2.get( if (s2.putback('Y')) // cannot modify input-only buffer std::cout << s2.rdbuf() << '\n'; else std::cout << "putback failed\n"; s2.clear( if (s2.putback('H')) // non-modifying putback std::cout << s2.rdbuf() << '\n'; else std::cout << "putback failed\n"; }

二次

产出:

二次

Yello, world putback failed Hello, world

二次

另见

sputbackcputs one character back in the input sequence (public member function of std::basic_streambuf)
ungetunextracts a character (public member function)
peekreads the next character without extracting it (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]iStream/putback