STL Copy

D

Derek Baker

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.php?thread_id=973964&forum_id=48211

Thanks in advance.
 
V

Victor Bazarov

Derek Baker said:
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.php?thread_id=973964&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
 
D

Derek Baker

Victor Bazarov said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top