map, copy et ostream_iterator
Bonsoir à tous
Un petit problème que l'on m'a posé que le chat, et je dois bien avouer que quelque chose m'échappe
bredelet a essayé d'utiliser copy d'une map sur cout avec ostream_iterator et un operateur <<
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <string>
#include <map>
#include <iterator>
#include <iostream>
using namespace std;
typedef std::map<int, string, less<int> > MapIntString;
typedef MapIntString::iterator MapIntStringIt;
typedef MapIntString::value_type PairIntString;
ostream& operator<< (ostream& a, const PairIntString &b)
{
a << "Clef : " << b.first << " Valeur : " << b.second;
return a;
}
int main(int, char**)
{
MapIntString map1;
MapIntStringIt itCourant;
map1[1]="premier";
map1[2]="second";
map1[3]="troisieme";
std::copy(map1.begin(),
map1.end(),
ostream_iterator<PairIntString>(cout, " ZZ "));
} |
Citation:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stream_iterator.h:196: error: no match for ‘operator<<’ in ‘*((std::ostream_iterator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::char_traits<char> >*)this)->std::ostream_iterator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::char_traits<char> >::_M_stream << __value’
Par contre, en écrivant :
Code:
1 2 3 4 5 6 7
| namespace std {
ostream &operator<<(ostream &a, const PairIntString &b)
{
a << "Clef : " << b.first << " Valeur : " << b.second;
return a;
}
} |
ça passe
Quelqu'un a une idée de l'explication ?