471,082 Members | 924 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

How to use std::copy with std::map?

Should the following work? It does on some compilers. How can I get it to
work on g++ 3.2?
On g++ 3.2 it keeps telling me that there is no such function operator <<
for ostream and pair<string,string>. But there is!
And I can call it! And it works! So why cannot std::copy find it?

thanks,
Siegfried

template<typename K, typename V>
ostream& operator<<(ostream& os, std::pair<K,V>& p){ return os << p.first <<
p.second; }

std::map<string,string> m; m[(string)"hello"] = (string)"there";
std::copy(m.begin(), m.end(), ostream_iterator<std::pair<string,string>
(cout, ",");

May 3 '06 #1
1 2680
Siegfried Heintze wrote:
Should the following work? It does on some compilers. How can I get
it to work on g++ 3.2?
Compiler-specific questions should be asked in the newsgroup dedicated
to that compiler. Try gnu.g++.help.
On g++ 3.2 it keeps telling me that there is no such function
operator << for ostream and pair<string,string>. But there is!
Where?
And I can call it! And it works! So why cannot std::copy find it?

thanks,
Siegfried

template<typename K, typename V>
ostream& operator<<(ostream& os, std::pair<K,V>& p){ return os <<
p.first << p.second; }
Is that what you wrote? Where does this definition appear? In
the global scope?
std::map<string,string> m; m[(string)"hello"] = (string)"there";
std::copy(m.begin(), m.end(),
ostream_iterator<std::pair<string,string>
(cout, ",");


Argument-dependent lookup (ADL) does not look in all the namespaces.
It limits its search to the types involved and the namespaces where
those types are defined. You need to place your operator in 'std'
namespace.

Next time please follow the recommendations of FAQ 5.8.

V
--
Please remove capital As from my address when replying by mail
May 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by sks_cpp | last post: by
8 posts views Thread by Jeff | last post: by
2 posts views Thread by Serengeti | last post: by
1 post views Thread by Saeed Amrollahi | last post: by
19 posts views Thread by Erik Wikström | last post: by
13 posts views Thread by kamaraj80 | last post: by
8 posts views Thread by mveygman | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.