You do not need to specialize anything. All you have to do is provide operator<< overloaded output operator<< for std::pair , as shown below:
template<typename T1, typename T2> std::ostream& operator<<(std::ostream &out, std::pair<T1, T2> const &mp) { return (out << "(" << mp.first << ", " << mp.second << ")"); }
Live demo
edit:
The above solution, however, like @Benjamin Lindley suggested in the comments, may conflict with other operator<< output template overloads for std::pair .
If so, you can also write in your own namespace (e.g. namespace detail ) two template overload functions (e.g. print_elem ), as shown below:
namespace detail { template<typename T1, typename T2> std::ostream& print_elem(std::ostream &out, std::pair<T1, T2> const &mp) { return (out << "(" << mp.first << ", " << mp.second << ")"); } template<typename T> std::ostream& print_elem(std::ostream &out, T const &elem) { return (out << elem); } }
and change the template print as follows:
template<typename Iterator> void print(Iterator begin, Iterator end) { while (begin != end) { detail::print_elem(cout, *begin) << endl; ++begin; } }
Live demo
101010
source share