// main.cpp
#include <iostream>
#include <utility>
#include <algorithm>
#include <iterator>
#include <map>
template<typename t1, typename t2>
std::ostream& operator<<(std::ostream& os, const std::pair<t1, t2>& pair)
{
return os << "< " << pair.first << " , " << pair.second << " >";
}
int main()
{
std::map<int, int> map = { { 1, 2 }, { 2, 3 } };
std::cout << *map.begin() << std::endl;//This works
std::copy(
map.begin(),
map.end(),
std::ostream_iterator<std::pair<int,int> >(std::cout, " ")
); //this doesn't work
}
出现了错误
无匹配的运算符'<<' (操作数类型为'std::ostream_iterator<std::pair<int, int> >::ostream_type {aka std::basic_ostream<char>} '和'const std::pair<int, int>')
我猜这不起作用是因为我的重载在 std::copy
内不可用,但是为什么呢?