首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_istream::sentry

class sentry;

?

?

类对象basic_istream::sentry的每个成员函数开始时,在局部范围内构造std::basic_istream执行输入%28的格式化和未格式化的%29。它的构造函数准备输入流:检查流是否已经处于失败状态,刷新Tie%28%29%27D输出流,跳过前导空格,除非noskipws标记被设置,并在必要时执行其他实现定义的任务。如果有必要,所有清理都在析构函数中执行,以便保证在输入期间抛出异常时会发生。

成员类型

traits_type

Traits

成员函数

(constructor)

constructs the sentry object. All the preparation tasks are done here (public member function)

(destructor)

finalizes the stream object after formatted input or after exception, if necessary (public member function)

operator= deleted

not copy assignable (public member function)

operator bool

checks if the preparation of the stream object was successful (public member function)

性病:基本[医]IStream::哨兵::哨兵

explicit sentry(std::basic_istream<CharT,Traits>& is, bool noskipws = false);

?

?

为格式化输入准备流。

如果is.good()false,电话is.setstate(failbit)%28自c++11%29并返回。否则,如果is.tie()不是空指针,调用is.tie()->flush()将输出序列与外部流同步。属性的PUT区域可以抑制此调用。is.tie()是空的。实现可能会将调用推迟到flush直到...的召唤is.rdbuf()->underflow()会发生。如果在哨兵对象被销毁之前没有发生这样的调用,则可以完全消除它。

如果noskipws是零和is.flags() & ios_base::skipws为非零,该函数提取并丢弃所有空白字符,直到下一个可用字符不是由当前注入的区域设置确定的空白字符%28。is29%。如果is.rdbuf()->sbumpc()is.rdbuf()->sgetc()回报traits::eof(),函数调用setstate(failbit | eofbit)%28std::ios_base::failure29%。

额外的实现---定义的准备工作可能会进行,这可能需要setstate(failbit)%28std::ios_base::failure29%。

如果准备完成后,is.good() == true,然后任何后续的调用operator bool会回来true...

参数

is

-

input stream to prepare

noskipws

-

true if whitespace should not be skipped

例外

std::ios_base::failure如果跳过空格时出现文件结束情况。

性病:基本[医]IStream::哨兵::~哨兵

~sentry();

?

?

什么都不做。

性病:基本[医]IStream::Sentry::Operator bool

explicit operator bool() const;

?

?

检查输入流的准备是否成功。

参数

%280%29

返回值

true如果输入流的初始化成功,false否则。

二次

代码语言:javascript
复制
#include <iostream>
#include <sstream>
 
struct Foo
{
   char n[5];
};
 
std::istream& operator>>(std::istream& is, Foo& f)
{
    std::istream::sentry s(is);
    if (s)
        is.read(f.n, 5);
    return is;
}
 
int main()
{
    std::string input = "   abcde";
    std::istringstream stream(input);
    Foo f;
    stream >> f;
    std::cout.write(f.n, 5);
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
abcde

二次

另见

operator>>

extracts formatted data (public member function)

operator>>(std::basic_istream)

extracts characters and character arrays (function template)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com