std::basic_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,则错误状态标志不是goodbit
29%和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
二次
另见
sputbackc | puts one character back in the input sequence (public member function of std::basic_streambuf) |
---|---|
unget | unextracts a character (public member function) |
peek | reads the next character without extracting it (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com