By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

STL Copy

P: n/a
Can anyone see anything wrong with this from a standard C++ point of view:

#include <iostream>
#include <string>
#include <map>
#include <iterator>
using namespace std;

void addToMap(map<int,string>& m,int index, const char* str)
{
string strcast(str);
pair<int,string> p (index,str);
m.insert(p);
}

ostream& operator << (ostream& os, const pair<int,string>& p)
{
os << p.first << ": " << p.second << endl;
return os;
}

int main(void)
{
map<int, string> strings;
addToMap(strings,3,"Hello");
addToMap(strings,5,"World");

ostream_iterator< pair<int,string> > out_it(cout,"");
copy(strings.begin(),strings.end(),out_it); /* won't compile */

return 0;
}

This is not actually my code, but that of a poster to a forum. After
screwing up a response to that post, I'd like to help find an answer. :)

If you want to see the thread:

http://sourceforge.net/forum/forum.p...forum_id=48211

Thanks in advance.
--
Derek
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Derek Baker" <me@XYZderekbaker.eclipse.co.uk> wrote...
Can anyone see anything wrong with this from a standard C++ point of view:

#include <iostream>
#include <string>
#include <map>
#include <iterator>
using namespace std;

void addToMap(map<int,string>& m,int index, const char* str)
{
string strcast(str);
pair<int,string> p (index,str);
m.insert(p);
}

ostream& operator << (ostream& os, const pair<int,string>& p)
{
os << p.first << ": " << p.second << endl;
return os;
}

int main(void)
{
map<int, string> strings;
addToMap(strings,3,"Hello");
addToMap(strings,5,"World");

ostream_iterator< pair<int,string> > out_it(cout,"");
copy(strings.begin(),strings.end(),out_it); /* won't compile */

return 0;
}

This is not actually my code, but that of a poster to a forum. After
screwing up a response to that post, I'd like to help find an answer. :)

If you want to see the thread:

http://sourceforge.net/forum/forum.p...forum_id=48211


The problem is that the compiler is not considering the global
namespace when it's looking for a suitable operator<<. It's a hole
in the look-up rules, or so I heard. It can be cured by an illegal
(from the standard point of view) trick: place the operator<< you
defined in the namespace std. The trick is illegal because you are
not supposed to put anything in std, only the implementation may.

Of course, it's entirely possible that I just don't remember the
legal way out of that problem.

Victor
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:AvBub.243377$Tr4.726518@attbi_s03...
"Derek Baker" <me@XYZderekbaker.eclipse.co.uk> wrote...
Can anyone see anything wrong with this from a standard C++ point of view:
#include <iostream>
#include <string>
#include <map>
#include <iterator>
using namespace std;

void addToMap(map<int,string>& m,int index, const char* str)
{
string strcast(str);
pair<int,string> p (index,str);
m.insert(p);
}

ostream& operator << (ostream& os, const pair<int,string>& p)
{
os << p.first << ": " << p.second << endl;
return os;
}

int main(void)
{
map<int, string> strings;
addToMap(strings,3,"Hello");
addToMap(strings,5,"World");

ostream_iterator< pair<int,string> > out_it(cout,"");
copy(strings.begin(),strings.end(),out_it); /* won't compile */

return 0;
}

This is not actually my code, but that of a poster to a forum. After
screwing up a response to that post, I'd like to help find an answer. :)

If you want to see the thread:

http://sourceforge.net/forum/forum.p...forum_id=48211


The problem is that the compiler is not considering the global
namespace when it's looking for a suitable operator<<. It's a hole
in the look-up rules, or so I heard. It can be cured by an illegal
(from the standard point of view) trick: place the operator<< you
defined in the namespace std. The trick is illegal because you are
not supposed to put anything in std, only the implementation may.

Of course, it's entirely possible that I just don't remember the
legal way out of that problem.

Victor


Thanks Victor,

Someone had actually posted the answer to the forum, just after my post
here. Though without your fuller explanation.

--
Derek
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.