Namespaces
Variants

std::basic_istream<CharT,Traits>:: unget

From cppreference.net
basic_istream & unget ( ) ;

使最近提取的字符再次可用。

首先清除 eofbit 。随后, (C++11 起) unget 表现为 未格式化输入函数 。在构造并检查哨兵对象后,若存在任何 ios_base::iostate 标志被设置,则函数设置 failbit 并返回。否则调用 rdbuf ( ) - > sungetc ( )

如果 rdbuf ( ) - > sungetc ( ) 返回 Traits :: eof ( ) ,则调用 setstate ( badbit )

在任何情况下,将 gcount() 计数器设置为零。

目录

参数

(无)

返回值

* this

异常

failure if an error occurred (the error state flag is not goodbit ) and exceptions() is set to throw for that state.

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

示例

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream s1("Hello, world.");
    char c1 = s1.get();
    if (s1.unget())
    {
        char c2 = s1.get();
        std::cout << "Got: '" << c1 << "'. Got again: '" << c2 << "'.\n";
    }
}

输出:

Got: 'H'. Got again: 'H'.

参见

将输入序列中的下一个指针向后移动一位
( std::basic_streambuf<CharT,Traits> 的公开成员函数)
提取字符
(公开成员函数)
读取下一个字符但不提取它
(公开成员函数)
将字符放回输入流
(公开成员函数)