c++boostiteratoristream-iterator

I can't understand the use of std::istream_iterator


I can't understand the code below.

(from https://www.boost.org/doc/libs/1_74_0/more/getting_started/unix-variants.html)

#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) << " " );
}

The web page doesn't explain anything for the code.

What I can't understand is the line with std::for_each function.

std::for_each is defined as below.

template <class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn);

So first is in(std::cin), last is just in(), the function is the cout statement.

Could anyone explain to me the syntax of first and last syntax and meaning in the example code?

The first iterator seems to be constructed with initial value std::cin, but what's the use of in() for the last value?

I also can't understand the _1 part.

The program outputs 3 * any number of integer values I type in.


Solution

  • Could anyone explain to me the syntax of first and last syntax and meaning in the example code?

    The first iterator seems to be constructed with initial value std::cin, but what's the use of in() for the last value?

    If you look at the description of the constructor of std::istream_iterator you can see that in() constructs the end-of-stream iterator.

    istream_iterator(); //  < C++11
    constexpr istream_iterator(); // > C++11
    

    Constructs the end-of-stream iterator, value-initializes the stored value. This constructor is constexpr if the initializer in the definition auto x = T(); is a constant initializer (since C++11).

    As for in(std::cin):

    istream_iterator( istream_type& stream );
    istream_iterator( const istream_iterator& other );  //< C++11
    istream_iterator( const istream_iterator& other ) = default; // > C++11
    

    Initializes the iterator, stores the address of stream in a data member, and performs the first read from the input stream to initialize the cached value data member.

    source

    And I also can't understand the _1 part.

    What this does is to replace the placeholder _1 with every element in the iteration sequence and multiply it by 3, using the result in the output stream, as it should, given the unary function argument.

    for_each(a.begin(), a.end(), std::cout << _1 << ' ');
    

    The expression std::cout << _1 << ' ' defines a unary function object. The variable _1 is the parameter of this function, a placeholder for the actual argument. Within each iteration of for_each, the function is called with an element of a as the actual argument. This actual argument is substituted for the placeholder, and the “body” of the function is evaluated.

    source