STL Copy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Baker

    STL Copy

    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<in t,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(string s,3,"Hello");
    addToMap(string s,5,"World");

    ostream_iterato r< pair<int,string > > out_it(cout,"") ;
    copy(strings.be gin(),strings.e nd(),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:



    Thanks in advance.
    --
    Derek


  • Victor Bazarov

    #2
    Re: STL Copy

    "Derek Baker" <me@XYZderekbak er.eclipse.co.u k> wrote...[color=blue]
    > 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<in t,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(string s,3,"Hello");
    > addToMap(string s,5,"World");
    >
    > ostream_iterato r< pair<int,string > > out_it(cout,"") ;
    > copy(strings.be gin(),strings.e nd(),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[/color]

    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


    Comment

    • Derek Baker

      #3
      Re: STL Copy

      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
      news:AvBub.2433 77$Tr4.726518@a ttbi_s03...[color=blue]
      > "Derek Baker" <me@XYZderekbak er.eclipse.co.u k> wrote...[color=green]
      > > Can anyone see anything wrong with this from a standard C++ point of[/color][/color]
      view:[color=blue][color=green]
      > >
      > > #include <iostream>
      > > #include <string>
      > > #include <map>
      > > #include <iterator>
      > > using namespace std;
      > >
      > > void addToMap(map<in t,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(string s,3,"Hello");
      > > addToMap(string s,5,"World");
      > >
      > > ostream_iterato r< pair<int,string > > out_it(cout,"") ;
      > > copy(strings.be gin(),strings.e nd(),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[/color]
      >
      > 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
      >
      >[/color]

      Thanks Victor,

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

      --
      Derek


      Comment

      Working...