I have a question with iterators of istream in C ++ 14

0

I have the following lines:

using par = std::pair<std::string, unsigned>;
std::istream& operator>>( std::istream&is, par & p ) {
   is >> p.first >> p.second;
   return is;
}

std::ostringstream out( "qwe 123\nasd 234", std::ios::out | std::ios::in );
std::istream in( out.rdbuf() );

std::copy( std::istream_iterator<par>( in ), std::istream_iterator<par>(),
   std::ostream_iterator<par>( std::cout, "\t" ) );

And it does not work for me. What is the problem with this algorithm?  I leave a link to a web editor and compiler. link to the example

    
asked by Emanuel Gauler 08.02.2018 в 00:27
source

1 answer

2

The problem is your std::istream& operator>>( std::istream&is, par & p ) is not in the namespace std, or in any of the associated namespaces, which is where you are going to search following the rules of the "Argument-dependent lookup" [ADL] ( link )

There is a trick that allows the ADL mechanism to find your overloaded operator, using its own type:

struct P : par {
    using par::pair;
};

Putting P instead of pair, it has to work. Something like this:

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;

using word = std::string;
using number = float;

using par = std::pair<word, number>;
using pares = std::vector<par>;


struct P : par {
    using par::pair;
};

std::ostream& operator<<(std::ostream&os, P const&p)
{
    os << p.first << " " << p.second;
    return os;
}

std::istream& operator>>(std::istream& is, P& p)
{
    is >> p.first >> p.second;
    return is;
}



int main()
{

    ostringstream os("qwe 123\nasd 321", ios::out | ios::in);
    istream in(os.rdbuf());

    std::copy(std::istream_iterator<P>(in), std::istream_iterator<P>(),
              std::ostream_iterator<P>(std::cout, "\t"));

    return EXIT_SUCCESS;
}
    
answered by 08.02.2018 / 01:36
source