overloading ostream_iterator for custom types for printing

Viewed 18
#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>


using namespace std;

typedef pair<int, int> PAIR;

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

int main()
{
    vector<PAIR > v{ {1,3},{2,3}, {1,0}, {1,5} };
    auto x1 = v;
    cout << "v: ";
    //std::copy(v.begin(), v.end(), ostream_iterator< PAIR >(cout, ", "));
    for (unsigned int i = 0; i < v.size(); i++) {
        cout << v[i]<<",";
    }
    cout << "\n";
}

OUTPUT v: (1, 3),(2, 3),(1, 0),(1, 5),

I would like to get similar output with commented line std::copy(v.begin(), v.end(), ostream_iterator< PAIR >(cout, ", "));

But I am getting the following error. binary 'operator' : no operator found which takes a right-hand operand of type 'type' (or there is no acceptable conversion)

Above error is not comprehensible to me. Any help or pointers is greatly appreciated. Thanks in advance.

0 Answers
Related