Namespaces
Variants

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

From cppreference.net
int_type peek ( ) ;

表现为 非格式化输入函数 。构造并测试岗哨对象后,从输入流中读取下一个字符但不提取该字符。

目录

参数

(无)

返回值

如果 good ( ) == true ,返回通过 rdbuf ( ) - > sgetc ( ) 获取的下一个字符。

否则,返回 Traits :: eof ( )

异常

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.peek();
    char c2 = s1.get();
    std::cout << "Peeked: " << c1 << " got: " << c2 << '\n';
}

输出:

Peeked: H got: H

参见

从输入序列读取一个字符但不推进序列
( std::basic_streambuf<CharT,Traits> 的公开成员函数)
提取字符
(公开成员函数)
回退一个字符
(公开成员函数)