我正在阅读“Accelerated C++”一书的第 8 章。 8.3 节是关于输入和输出迭代器的:
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(v));
[...]
The second argument to copy creates a default (empty) istream_iterator, which is not bound to any file. The istream_iterator type has a default value with the property that any istream_iterator that has reached end-of-file or is in an error state will appear to be equal to the default value. Therefore, we can use the default value to indicate the "one-past-the-end" convention for copy.
我是这样理解的:istream_iterator是一个模板类,istream_iterator
vector<int> v; // read ints from the standard input and append them to v copy(istream_iterator<int>(cin), istream_iterator<int>, back_inserter(v));
但这不编译:
error: expected primary-expression before ‘,’ token
我不明白这是怎么回事。欢迎任何解释。
最佳答案
没有办法默认初始化,而不是值初始化,一个临时的。虽然表达式 type()
创建了一个值初始化的临时值,但类型名称本身并不是一个有效的表达式。
但是,对于声明默认构造函数的任何类型(像这个),默认初始化和值初始化是等价的;在调用非隐式构造函数之前没有零初始化。
关于c++ - 输入迭代器的值初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18019489/