std :: istreambuf_iterator< char>没有参数初始化? [英] std::istreambuf_iterator<char> initializes with no arguments?
问题描述
这是代码:
#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>());
}
根据参考,vector.assign
函数采用两个参数,first
和last
,并将两者之间的任何内容带入向量中. istreambuf_iterator函数采用以下形式:
According to the reference, the vector.assign
function takes two arguments, first
and last
, and takes anything in between into the vector. And the istreambuf_iterator function takes this form:
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?
还要注意,bytes
的类型是unsigned char
,而迭代器的类型是char
,这不是不匹配吗?
Also notice that the type of bytes
is unsigned char
, while the type of the iterator is char
, isn't this a mismatch?
推荐答案
第二个迭代器初始值设定项不带参数,这是什么意思?
the second iterator initializer takes no arguments, what does it mean?
这意味着它已初始化为流结束迭代器.
It means it's initialized to be the end-of-stream iterator.
还要注意,
bytes
的类型是unsigned int
,而迭代器的类型是char
,这不匹配吗?
Also notice that the type of
bytes
isunsigned int
, while the type of the iterator ischar
, isn't this a mismatch?
您是说unsigned char
对吗? (这就是您的代码中所说的.)
You mean unsigned char
right? (That's what it says in your code.)
这很好,因为可以从char
构造和分配unsigned char
.具有迭代器范围的模板化函数通常不需要类型完全匹配. (有关确切要求,请参阅标准§23.2.3中的表100.)
It's fine because unsigned char
can be constructed from and assigned from 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.)
这篇关于std :: istreambuf_iterator< char>没有参数初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!