Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我能';我不理解std::istream_迭代器的用法_C++_Boost_Iterator_Istream Iterator - Fatal编程技术网

C++ 我能';我不理解std::istream_迭代器的用法

C++ 我能';我不理解std::istream_迭代器的用法,c++,boost,iterator,istream-iterator,C++,Boost,Iterator,Istream Iterator,我无法理解下面的代码 (来自) 因此,第一个是在(std::cin)中,最后一个只是在()中,函数是cout语句 有人能给我解释一下示例代码中first和last的语法和含义吗 第一个迭代器似乎是用初始值std::cin构造的,但是()中的对于最后一个值有什么用呢 我也不能理解\u 1部分 程序输出我输入的任意数量的整数值。首先对函数进行一点解释 函数从头到尾在一组迭代器上循环,为范围内的每个元素调用函数 如果你有一个整数向量: std::vector<int> v = { 1, 2

我无法理解下面的代码

(来自)

因此,
第一个
在(std::cin)
中,
最后一个
只是
在()
中,
函数
cout
语句

有人能给我解释一下示例代码中
first
last
的语法和含义吗

第一个
迭代器似乎是用初始值
std::cin
构造的,但是()中的
对于最后一个值有什么用呢

我也不能理解
\u 1
部分


程序输出我输入的任意数量的整数值。

首先对函数进行一点解释

函数从头到尾在一组迭代器上循环,为范围内的每个元素调用函数

如果你有一个整数向量:

std::vector<int> v = { 1, 2, 3, 4 };

现在,如果我们讨论问题中的用法,它将使用迭代器包装输入运算符
>

<> >改写<代码> STD::FuyAuth/Engult>使用标准C++ LAMBDAS调用,它看起来是这样的:

std::for_each(in(std::cin), in(), [](int value) { std::cout << (value * 3) << " " ); });
它的作用是从
std::cin
读取整数输入(直到文件结束或出现错误),然后输出值乘以
3
和一个空格


如果您想知道(std::cin)中的
和(
)中的
,您必须记住,
中的
是类型
std::istream\u迭代器的别名

这意味着
in(std::cin)
std::istream\u迭代器(std::cin)
相同。即,它创建一个
std::istream\u迭代器
对象,并将
std::cin
传递给构造函数。和
in()
构造一个结束迭代器对象

更清楚地说,该准则相当于:

for (auto i = v.begin(); i != v.end(); ++i)
{
    std::cout << *i;
}
std::istream_iterator<int> the_beginning(std::cin);
std::istream_iterator<int> the_end;  // Default construct, becomes the "end" iterator

for (std::istream_iterator<int> i = the_beginning; i != the_end; ++i)
{
    int value = *i;  // Dereference iterator to get its value
                     // (effectively the same as std::cin >> value)

    std::cout << (value * 3) << " ";
}
std::istream_迭代器开始(std::cin);
std::istream\u迭代器\u end;//默认构造,成为“结束”迭代器
for(std::istream\u迭代器i=开始;i!=结束;++i)
{
int value=*i;//取消引用迭代器以获取其值
//(实际上与std::cin>>值相同)
标准::cout
有人能给我解释一下示例代码中
first
last
的语法和含义吗

第一个
迭代器似乎是用初始值
std::cin
构造的,但是()中的
对于最后一个值有什么用呢

如果您查看
std::istream_iterator
的构造函数描述,您可以看到
in()
构造流结束迭代器

初始化迭代器,将流的地址存储在数据成员中,并从输入流执行第一次读取以初始化缓存值数据成员

我也不能理解
\u1
部分

这样做的目的是用迭代序列中的每个元素替换占位符
\u 1
,并使用输出流中的结果将其乘以3,这在给定一元函数参数时是应该的


for_each(a.begin()、a.end()、std::cout阅读关于
istream_迭代器的构造函数。还有一个类似的例子:in()构造流的结尾迭代器以及boost::lambda::_1是一个占位符,它创建了一个需要一个参数的lambda函数。啊,谢谢(我正在阅读cppreferece页面,发现)@bakaDev你能把它作为一个答案发布给我吗?谢谢你的善意解释!(但我知道迭代器的基本用法,你错过了对_1的解释,所以我选择了另一个答案。)@ChanKim,不客气,是的,你是对的,这是boost::lambda::Placeholder 1_type&boost::lambda:::_1
,但基本上是一样的。
for (auto i = v.begin(); i != v.end(); ++i)
{
    std::cout << *i;
}
std::for_each(in(std::cin), in(), [](int value) { std::cout << (value * 3) << " " ); });
for (auto i = in(std::cin); i != in(); ++i)
{
    std::cout << (*i * 3) << " ";
}
std::istream_iterator<int> the_beginning(std::cin);
std::istream_iterator<int> the_end;  // Default construct, becomes the "end" iterator

for (std::istream_iterator<int> i = the_beginning; i != the_end; ++i)
{
    int value = *i;  // Dereference iterator to get its value
                     // (effectively the same as std::cin >> value)

    std::cout << (value * 3) << " ";
}
istream_iterator(); //  < C++11
constexpr istream_iterator(); // > C++11
istream_iterator( istream_type& stream );
istream_iterator( const istream_iterator& other );  //< C++11
istream_iterator( const istream_iterator& other ) = default; // > C++11
for_each(a.begin(), a.end(), std::cout << _1 << ' ');