iterating through a set<string> when passed by reference

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

    #1

    iterating through a set<string> when passed by reference

    The following code will compile. I have one line commented out because it
    won't compile. Why can I not do this:

    //set<string>::it erator iterator = s->begin();

    Why do I have to deference the set when I receive it as a pointer? Is that
    just the way we are supposed to do it, or am I missing something?

    -------------------------------------------------

    #include <iostream>
    #include <set>
    #include <string>
    using namespace std;

    typedef set<stringStrin gSet;

    void zoot(set<string * s);

    int main()
    {
    set<strings;
    s.insert("aaa") ;
    s.insert("bbb") ;
    zoot( &s );
    return 0;
    }

    void zoot(set<string * s )
    {
    // This won't compile - why?
    //set<string>::it erator iterator = s->begin();

    // dereference s, use s1, and it compiles
    set<strings1 = *s;
    for( set<string>::it erator iterator1 = s1.begin();iter ator1 !=
    s1.end();iterat or1++)
    {
    cout << *iterator1;
    }
    }


  • Ismo Salonen

    #2
    Re: iterating through a set&lt;string&g t; when passed by reference

    Zootal wrote:
    The following code will compile. I have one line commented out because it
    won't compile. Why can I not do this:
    >
    //set<string>::it erator iterator = s->begin();
    >
    Why do I have to deference the set when I receive it as a pointer? Is that
    just the way we are supposed to do it, or am I missing something?
    >
    -------------------------------------------------
    >
    #include <iostream>
    #include <set>
    #include <string>
    using namespace std;
    >
    typedef set<stringStrin gSet;
    >
    void zoot(set<string * s);
    >
    int main()
    {
    set<strings;
    s.insert("aaa") ;
    s.insert("bbb") ;
    zoot( &s );
    return 0;
    }
    >
    void zoot(set<string * s )
    {
    // This won't compile - why?
    //set<string>::it erator iterator = s->begin();
    >
    // dereference s, use s1, and it compiles
    set<strings1 = *s;
    for( set<string>::it erator iterator1 = s1.begin();iter ator1 !=
    s1.end();iterat or1++)
    {
    cout << *iterator1;
    }
    }
    >
    >
    What compiler and what error message ?

    You could try (*s).begin() , MSVC6 had problems with s-syntax
    sometimes, it did not correctly implement templates for obvious reasons.

    ismo

    Comment

    • advice.of.deal@gmail.com

      #3
      Re: iterating through a set&lt;string&g t; when passed by reference

      I use MSVC6 to compile your code. It's OK. There is not any complile
      errors. (before compile I and a '}' and then restore the line that you
      comment out )




      On 3ÔÂ13ÈÕ, ÏÂÎç12ʱ05·Ö, "Zootal" <nousenets...@z ootal.nospam.co mwrote:
      The following code will compile. I have one line commented out because it
      won't compile. Why can I not do this:
      >
      //set<string>::it erator iterator = s->begin();
      >
      Why do I have to deference the set when I receive it as a pointer? Is that
      just the way we are supposed to do it, or am I missing something?
      >
      -------------------------------------------------
      >
      #include <iostream>
      #include <set>
      #include <string>
      using namespace std;
      >
      typedef set<stringStrin gSet;
      >
      void zoot(set<string * s);
      >
      int main()
      {
      set<strings;
      s.insert("aaa") ;
      s.insert("bbb") ;
      zoot( &s );
      return 0;
      >
      }
      >
      void zoot(set<string * s )
      {
      // This won't compile - why?
      //set<string>::it erator iterator = s->begin();
      >
      // dereference s, use s1, and it compiles
      set<strings1 = *s;
      for( set<string>::it erator iterator1 = s1.begin();iter ator1 !=
      s1.end();iterat or1++)
      {
      cout << *iterator1;
      }
      >
      >
      >
      }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -

      Comment

      • Cari Elf

        #4
        Re: iterating through a set&lt;string&g t; when passed by reference

        Have you tried passing the set by reference?

        void zoot(set<string & s )
        {
        set<string>::it erator it;
        for (it = s.begin(); it != s.end(); ++it)
        {
        cout << *it;
        }
        }

        Comment

        • gethostbyname@gmail.com

          #5
          Re: iterating through a set&lt;string&g t; when passed by reference

          On 13 mar, 01:05, "Zootal" <nousenets...@z ootal.nospam.co mwrote:
          The following code will compile. I have one line commented out because it
          won't compile. Why can I not do this:
          >
          //set<string>::it erator iterator = s->begin();
          >
          Why do I have to deference the set when I receive it as a pointer? Is that
          just the way we are supposed to do it, or am I missing something?
          >
          -------------------------------------------------
          >
          #include <iostream>
          #include <set>
          #include <string>
          using namespace std;
          >
          typedef set<stringStrin gSet;
          >
          void zoot(set<string * s);
          >
          int main()
          {
          set<strings;
          s.insert("aaa") ;
          s.insert("bbb") ;
          zoot( &s );
          return 0;
          >
          }
          >
          void zoot(set<string * s )
          {
          // This won't compile - why?
          //set<string>::it erator iterator = s->begin();
          >
          // dereference s, use s1, and it compiles
          set<strings1 = *s;
          for( set<string>::it erator iterator1 = s1.begin();iter ator1 !=
          s1.end();iterat or1++)
          {
          cout << *iterator1;
          }
          >
          }
          It compiles here, VS 2005.

          gethostbyname

          Comment

          • Ook

            #6
            Re: iterating through a set&lt;string&g t; when passed by reference

            >
            It compiles here, VS 2005.
            >
            I'm using VS2003. I tried it on my desktop, and it compiles fine. My
            laptop has VS2003 academic version, and it gives an error - I don't
            have it with me now so I can't post the actual error, but I can do
            that tonight. WTF? Is anyone aware of any differences between the
            academic version and the retail version?

            Comment

            • Ook

              #7
              Re: iterating through a set&lt;string&g t; when passed by reference

              You could try (*s).begin() , MSVC6 had problems with s-syntax
              sometimes, it did not correctly implement templates for obvious reasons.
              >
              ismo
              Interesting enough, that fixed another problem I had. I was doing
              this:

              Item* bb = *iterator;

              And using bb because I couldn't figure out how to use the item the
              iterator was pointing to, until I figured out I can use (*iterator)
              instead. Thanks for the info :)

              Comment

              • Victor Bazarov

                #8
                Re: iterating through a set&lt;string&g t; when passed by reference

                Ook wrote:
                >It compiles here, VS 2005.
                >>
                >
                I'm using VS2003. I tried it on my desktop, and it compiles fine. My
                laptop has VS2003 academic version, and it gives an error - I don't
                have it with me now so I can't post the actual error, but I can do
                that tonight. WTF? Is anyone aware of any differences between the
                academic version and the retail version?
                People in 'microsoft.publ ic.vc.language' probably are.

                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask


                Comment

                • Zootal

                  #9
                  Re: iterating through a set&lt;string&g t; when passed by reference


                  "Zootal" <nousenetspam@z ootal.nospam.co mwrote in message
                  news:AZ6dnQeW8a uavmvYnZ2dnUVZ_ qSrnZ2d@giganew s.com...
                  The following code will compile. I have one line commented out because it
                  won't compile. Why can I not do this:
                  >
                  //set<string>::it erator iterator = s->begin();
                  >
                  <snip>

                  So I get home tonight, load the project, and it compiles. I spent an hour
                  banging my head against the wall, wondering why it would not compile. I'm
                  loosing it. Microsoft products hate me because I run linux on my server,
                  that must be it...


                  Comment

                  • gethostbyname@gmail.com

                    #10
                    Re: iterating through a set&lt;string&g t; when passed by reference

                    On 13 mar, 22:11, "Zootal" <nousenets...@z ootal.nospam.co mwrote:
                    "Zootal" <nousenets...@z ootal.nospam.co mwrote in message
                    >
                    news:AZ6dnQeW8a uavmvYnZ2dnUVZ_ qSrnZ2d@giganew s.com...The following code will compile. I have one line commented out because it
                    won't compile. Why can I not do this:
                    >
                    //set<string>::it erator iterator = s->begin();
                    >
                    <snip>
                    >
                    So I get home tonight, load the project, and it compiles. I spent an hour
                    banging my head against the wall, wondering why it would not compile. I'm
                    loosing it. Microsoft products hate me because I run linux on my server,
                    that must be it...
                    IMHO, if you would like to work with VS, use the 2005 Edition. Or use
                    gcc on Win :-)

                    gethostbyname

                    Comment

                    Working...