简体   繁体   English

std :: istreambuf_iterator <char> 没有参数初始化?

[英]std::istreambuf_iterator<char> initializes with no arguments?

Here is the code: 这是代码:

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>

std::vector<unsigned char> bytes;
{
    std::ifstream in(name, std::ios_base::binary);
    bytes.assign(std::istreambuf_iterator<char>(in >> std::noskipws),
                 std::istreambuf_iterator<char>());
}

According to the reference, the vector.assign function takes two arguments, first and last , and takes anything in between into the vector. 根据参考资料, vector.assign函数接受两个参数firstlast ,并将两者之间的任何内容都纳入向量。 And the istreambuf_iterator function takes this form: istreambuf_iterator函数采用以下形式:

istreambuf_iterator( std::basic_istream<CharT,Traits>& is );    
istreambuf_iterator( std::basic_streambuf<CharT,Traits>* s );

These are all easy to understand, but in the snippet above, the second iterator initializer takes no arguments, what does it mean? 这些都很容易理解,但是在上面的代码段中,第二个迭代器初始值设定项不带参数,这是什么意思?

Also notice that the type of bytes is unsigned char , while the type of the iterator is char , isn't this a mismatch? 还要注意, bytes类型是unsigned char ,而迭代器的类型是char ,这不匹配吗?

the second iterator initializer takes no arguments, what does it mean? 第二个迭代器初始值设定项不带参数,这是什么意思?

It means it's initialized to be the end-of-stream iterator. 这意味着它已初始化为流结束迭代器。

Also notice that the type of bytes is unsigned int , while the type of the iterator is char , isn't this a mismatch? 还要注意, bytes类型是unsigned int ,而迭代器的类型是char ,这不匹配吗?

You mean unsigned char right? 您是说unsigned char吗? (That's what it says in your code.) (这就是您的代码中所说的。)

It's fine because unsigned char can be constructed from and assigned from char . 这很好,因为unsigned char可以从构造和分配的char Templated functions taking iterator ranges generally do not require that the types match exactly. 具有迭代器范围的模板化函数通常不需要类型完全匹配。 (For the precise requirements, see Table 100 in §23.2.3 of the standard.) (有关确切要求,请参阅该标准的§23.2.3中的表100。)

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

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