Build a Simple Program Using Boost in(std::cin) : What does it mean?
2013-01-31 12:40
686 查看
From:http://stackoverflow.com/questions/8262994/instdcin-what-does-it-mean
In the first example of Boost,
used. I think
an
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:
otice the typedef in the code:
Thus,
the same as
it's calling the constructor for that type. There is a 1-argument constructor which accepts a
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
iterate over every value provided by
now until it runs out.
a stream and provides an iterator over the
in the stream, using
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
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 ishttp://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
of just talking about formatted I/O operations (which is what
Next comes a page with some C++ example snippets, then a couple of StackOverflow questions where people were trying to do something similar, etc....
just a
the example is just calling
a "range" defined by the two temporary iterators:
A value-inititalized
just a universal "end" iterator for streams.
How
from the
it ensures that the operators
then
the
are used, rather than an
acts directly on a
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
Thus
an iterator over integers, used by
that reads from stdin (cin) and multiplies them by 3 and prints them out.
In the first example of Boost,
in(std::cin)is
used. I think
in()get
an
istreamand
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) << " " ); }
otice 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_eachwill
iterate over every value provided by
std::cinfrom
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 ishttp://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....
inis
just a
typedeffor
std::istream_iterator<int>so
the example is just calling
std::for_eachon
a "range" defined by the two temporary iterators:
std::istream_iterator<int>(std::cin)and
std::istream_iterator<int>().
A value-inititalized
istream_iteratoris
just a universal "end" iterator for streams.
How
std::cout << (_1 * 3) << " "works is more subtle. Because
_1comes
from the
boost::lambdanamespace,
it ensures that the operators
*and
then
<<from
the
boost::lambdanamespace
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.
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.
相关文章推荐
- What does it mean by rendering of page in JSF
- What does it mean by Single-Leg & Multiple Leg in Stock Trading?
- What does it mean to write `static void * ptr = &ptr` in objective-c?
- What does Using filesort mean in MySQL?
- When we call objective c is runtime language what does it mean? 我们说的oc是动态运行时语言是什么意思?
- What does "size" in int(size) of MySQL mean?
- What does AspCompat="true" mean and when should I use it?
- What does the 11 mean in INT(11)?
- (Kattis - heritage)What Does It Mean?(map+记忆话搜索)(补)
- java classpath: what does it mean?
- What does AspCompat="true" mean and when should I use it?
- what does mean "Units" in apple app sales report?
- what does it means for MODULE_LICENSE in linux kernel
- Chinese cyberattacks and a Presidential executive order: What does it mean for you?
- In C++, what’s a vtable and how does it work?
- Chinese cyberattacks and a Presidential executive order: What does it mean for you?
- What is SynchronizationContext, and what does it mean to capture and use it?
- Bunder: What does :require => nil in Gemfile mean?
- __memcpy_sse2_unaligned - what does this mean in detail?
- (转载)What does (Aborted) mean in HttpWatch?