Printing using ostream_iterator and copy

I can do this:
1
2
3
 vector<int> v;
//Code that fills up v
copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"));


Can I do something similar if I have a vector of some objects of type T that I've defined ?
I guess if my object X is of type T, then
cout << X needs to work for the copy code to work. How can I do that ?
Last edited on
Declare and implement operator<< for X.

1
2
3
struct X {
    friend std::ostream& operator<<( std::ostream& os, const X& x );
};

Thanks !
Topic archived. No new replies allowed.