简体   繁体   English

C++ 带 {} 的空迭代器

[英]C++ empty iterator with {}

I found this answer on stack overflow for reading a stream into a string .我在堆栈溢出中找到了这个答案,用于将 stream 读入字符串 The code std::string s(std::istreambuf_iterator<char>(stream), {});代码std::string s(std::istreambuf_iterator<char>(stream), {}); works just fine for what I was doing, but I was really confused about the {} .对我正在做的事情来说工作得很好,但我真的对{}感到困惑。 As far as I can tell this is using the std::string constructor that uses a begin and end iterator, but I've never seen an iterator defined with {} .据我所知,这是使用使用开始和结束迭代器的std::string构造函数,但我从未见过用{}定义的迭代器。 What does that mean and how is it achieving the same thing as std::istreambuf_iterator<char> eos;这是什么意思,它是如何实现与std::istreambuf_iterator<char> eos;相同的东西的? ?

{} is a braced initialiser list in this context. {}是此上下文中的花括号初始化程序列表。 It is a list of initialisers used to initialise an object. It is used to initialise a temporary object.它是用于初始化 object 的初始化程序列表。它用于初始化临时 object。

In this case, the list of initialisers is empty.在这种情况下,初始化列表是空的。 In such case, the object will be value initialised.在这种情况下,object 将被初始化。 In case of a non-trivially-default-constructible class such as std::istreambuf_iterator value initialisation means that the default constructor will be used.在非平凡默认可构造 class 的情况下,例如std::istreambuf_iterator值初始化意味着将使用默认构造函数。

A default consturcted std::istreambuf_iterator is a sentinel that represents end of stream. This is a feature specific to stream iterators and not to all other iterators.默认构造的std::istreambuf_iterator是表示 stream 结束的标记。这是特定于 stream 迭代器而非所有其他迭代器的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM