我有下一个代码:
#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
//namespace std
//{
std::ostream& operator << ( std::ostream& out,
const std::pair< size_t, size_t >& rhs )
{
out << rhs.first << ", " << rhs.second;
return out;
}
//}
int main()
{
std::map < size_t, size_t > some_map;
// fill some_map with random values
for ( size_t i = 0; i < 10; ++i )
{
some_map[ rand() % 10 ] = rand() % 100;
}
// now I want to output this map
std::copy(
some_map.begin(),
some_map.end(),
std::ostream_iterator<
std::pair< size_t, size_t > >( std::cout, "\n" ) );
return 0;
}
在这段代码中,我只想将映射复制到输出流。为此,我需要定义运算符 <<(..) - 好的。但是根据名称查找规则编译器找不到我的操作符<<()。
因为 std::cout、std::pair 和 std::copy 调用了我的 operator<< - 都来自命名空间 std。
快速解决方案 - 将我的 oerator<< 添加到 std 命名空间 - 但它很难看,恕我直言。
您知道此问题的哪些解决方案或解决方法?