in(std::cin):这是什么意思?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

In the first example of Boost, in(std::cin) is used. I think in() get an istream and create some kind of iterator. However, I could not find any C++ documentation that explain it in detail. Could you please help me to find one?

here is the copy and paste of the example from the Boost webpage:

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
   using namespace boost::lambda;
   typedef std::istream_iterator<int> in;
   std::for_each(
   in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

#0

in is just a typedef for std::istream_iterator<int> so the example is just calling std::for_each on a "range" defined by the two temporary iterators: std::istream_iterator<int>(std::cin) and std::istream_iterator<int>().

A value-inititalized istream_iterator is just a universal "end" iterator for streams.

How std::cout << (_1 * 3) << " " works is more subtle. Because _1 comes from the boost::lambda namespace, it ensures that the operators * and then << from the boost::lambda namespace are used, rather than an operator<< that acts directly on a std::ostream. This way the whole expression becomes a lambda rather than (any part of it) being executed as a conventional expression at the call site of for_each.

#1

You defined it here:

typedef std::istream_iterator<int> in;

Thus in(std::cin) is an iterator over integers, used by std::for_each, that reads from stdin (cin) and multiplies them by 3 and prints them out.

#2

Notice the typedef in the code:

typedef std::istream_iterator<int> in;

Thus, in(...) is the same as std::istream_iterator<int>(...): it's calling the constructor for that type. There is a 1-argument constructor which accepts a std::istream, creating an iterator that represents the current point in that stream; and a 0-argument constructor creating an iterator that represents the end of any given stream. So std::for_each will iterate over every value provided by std::cin from now until it runs out.

std::istream_iterator<int> takes a stream and provides an iterator over the ints in the stream, using operator>> to read them out of the stream.

However, I could not find any C++ documentation that explain it in detail.

I don't know how you could possibly fail. I put std::istream_iterator<int> into Google and the first result was http://www.sgi.com/tech/stl/istream_iterator.html which is pretty thorough, assuming you're already familiar with iterators. The next result is http://www.cplusplus.com/reference/std/iterator/istream_iterator/ which makes another attempt to explain things and is also fully detailed. Next comes http://stdcxx.apache.org/doc/stdlibref/istream-iterator.html , similarly, which finally explicitly mentions operator>> instead of just talking about formatted I/O operations (which is what operator>> does). Next comes a page with some C++ example snippets, then a couple of StackOverflow questions where people were trying to do something similar, etc....

推荐文章

获取查询sqlserver中group by子句的第一行

获取查询sqlserver中group by子句的第一行

推荐文章

使用wmode=direct breaks express安装

使用wmode=direct breaks express安装

推荐文章

在监视接近度时是否可以不关闭显示器?

在监视接近度时是否可以不关闭显示器?

推荐文章

在Django中使用CouchDb作为后端实现身份验证/授权

在Django中使用CouchDb作为后端实现身份验证/授权

推荐文章

Perl-将Javascript操作提交到主机

Perl-将Javascript操作提交到主机

推荐文章

自动完成订购Visual Studio 2010

自动完成订购Visual Studio 2010

推荐文章

确保脚本可用性的模式或技术?

确保脚本可用性的模式或技术?

推荐文章

cursor.execute(“插入im_entry.test(“+entrym+”)值(“+p+”);”)

cursor.execute(“插入im_entry.test(“+entrym+”)值(“+p+”);”)

推荐文章

如何在SASS中创建资产?

如何在SASS中创建资产?

推荐文章

c#FileBrowse更改应用程序其余部分的路径

c#FileBrowse更改应用程序其余部分的路径

推荐文章

如何正确设置装载机的速度路径

如何正确设置装载机的速度路径

推荐文章

如何为给定的文件版本确定ClearCase目录版本?

如何为给定的文件版本确定ClearCase目录版本?

推荐文章

Visual Studio 2010:扩展/差异

Visual Studio 2010:扩展/差异

推荐文章

mb_send_mail中的Php错误

mb_send_mail中的Php错误

推荐文章

C#获取所有可用的FontFamily

C#获取所有可用的FontFamily

推荐文章

更改magento中共享愿望列表邮件的发件人电子邮件地址

更改magento中共享愿望列表邮件的发件人电子邮件地址