Re: overloading << for map and multimap simultaneously
On Jul 30, 12:39 pm, ozizus <o...@ce.yildiz.edu.tr> wrote:
On Jul 29, 10:34 pm, James Kanze <james.ka...@gmail.com> wrote:
On Jul 29, 3:00 pm, ozizus <o...@ce.yildiz.edu.tr> wrote:
I overloaded operator << for STL map successfully:
template <typename T1, typename T2> ostream & operator << (ostream &
o, map <T1,T2> & m)
the code works like a charm.
Are you sure? In what namespace did you put it?
I put it in no namespace. I use "using namespace std;". This may be
bad practice but it works.
No it doesn't. (I mean putting your operator in no namespace.)
Try something like:
std::ostream_iterator< std::map< X, Y > >( std::cout,
"\n" ) ) ;
Your operator won't be found, unless either X or Y are user
defined types in the same namespace as your operator.
Since I overloaded << for other containers,
I can output any object that is a combination of these containers in
one line. Same is true for >> too.
map<vector<int>, set<string>> m;
Very unmaintainable, you mean. Not something you really want to
do; it's a definite recepe for undefined behavior.
The answer to your question is simple: there's no way to define
an overload of operator<< for a standard type in a way that is
guaranteed to work in all cases, and you don't want to, even if
James Kanze (GABI Software) email:firstname.lastname@example.org
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34