Home > Back-end >  Get istream_iterator from ifstream
Get istream_iterator from ifstream

Time:08-16

I tried to get an istream iterator from an ifstream, but failed...

void Test_CH_3_1::set_up()
{
    std::ifstream file_in("makefile-dependencies.dat");
    typedef boost::graph_traits<Graph>::vertices_size_type size_type;
    size_type n_vertices;
    file_in >> n_vertices; // read in number of vertices
    if (file_in)
    {
        std::cout << n_vertices << std::endl;
    }

    std::istream_iterator<std::pair<size_type, size_type> > input_begin, input_end;
    input_begin=std::istream_iterator<std::pair<size_type, size_type> >(file_in);
    g_ = Graph(input_begin, input_end, n_vertices);
}

And I got below error:

no match for "operator>>" (operand types are std::istream_iterator<std::pair<long unsigned int, long unsigned int> >::istream_type {aka std::basic_istream<char> and std::pair<long unsigned int, long unsigned int>)

CodePudding user response:

The error message is quite clear. There is no operator>> defined for reading std::pair from a std::istream. Define one in your application code and all should be well.

std::istream& operator>>(std::istream& in, std::pair<std::size_type, std::size_type>& p)
{
   return in >> p.first >> p.second;
}
  • Related