を私は次のコードを持っている:私はちょうど出力ストリームにマップをコピーする場合は、このコードではのstd ::コピー先はstd :: coutでのstd ::ペア
#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;
}
を。このために私は演算子を定義する必要があります< <(..) - OK。 しかし、コンパイラが私のオペレータ< <()を見つけることができません。
ためのstd :: coutを、STD ::ペアと私のオペレータ< <と呼ばれるのstd ::コピー - すべての名前空間stdから。
クイックソリューション - 私のエイラーを< <に追加してください。しかし、それは醜いです。
この問題の解決方法または回避策はありますか。
ところで、最初のfor-loopを実行するSTLアルゴリズムがあります。http://www.sgi.com/tech/stl/generate.htmlを参照してください。ランダムな値にランダムな位置、http://www.sgi.com/tech/stl/random_shuffle.html。 – paxos1977