std::basic_streambuf::sputbackc
int_type sputbackc( char_type c );  |   |   | 
|---|
将字符返回到GET区域。
如果在GET区域%28中有一个备用位置gptr() > eback()%29,和字符c的左边的一个位置等于字符。gptr()28%Traits::eq(c, gptr()[-1]),然后简单地减少下一个指针%28gptr()29%。
否则,打电话pbackfail(Traits::to_int_type(c))要么备份GET区域,要么修改GET区域,并可能修改相关的字符序列。
I/O流函数basic_istream::putback是根据此功能实现的。
参数
%280%29
返回值
如果返回位置可用,则返回下一个指针指向的字符,转换为int_type带着Traits::to_int_type(*gptr())下一个单字符输入将返回这个字符。
如果没有备用位置,则返回pbackfail()返回,即Traits::eof()在失败的时候。
例
二次
#include <iostream>
#include <sstream>
 
int main()
{
    std::stringstream s("abcdef"); // gptr() points to 'a' in "abcdef"
    std::cout << "Before putback, string holds " << s.str() << '\n';
    char c1 = s.get(); // c1 = 'a', gptr() now points to 'b' in "abcdef"
    char c2 = s.rdbuf()->sputbackc('z'); // same as s.putback('z')
                                         // gptr() now points to 'z' in "zbcdef"
    std::cout << "After putback, string holds " << s.str() << '\n';
    char c3 = s.get(); // c3 = 'z', gptr() now points to 'b' in "zbcdef"
    char c4 = s.get(); // c4 = 'b', gptr() now points to 'c' in "zbcdef"
    std::cout << c1 << c2 << c3 << c4 << '\n';
 
    s.rdbuf()->sputbackc('b');  // gptr() now points to 'b' in "zbcdef"
    s.rdbuf()->sputbackc('z');  // gptr() now points to 'z' in "zbcdef"
    int eof = s.rdbuf()->sputbackc('x');  // nothing to unget: pbackfail() fails
    if (eof == EOF)
        std::cout << "No room to putback after 'z'\n";
}二次
产出:
二次
Before putback, string holds abcdef
After putback, string holds zbcdef
azzb
No room to putback after 'z'二次
另见
sungetc  | moves the next pointer in the input sequence back by one (public member function)  | 
|---|---|
putback  | puts character into input stream (public member function of std::basic_istream)  | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

