map question

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

    map question

    In the following program I am getting an error when I am trying to access
    i1.first. Is this valid statement?
    how can I access map value through iterator? Please help me foreach
    function also.


    void printall(string str, int i){
    cout << str<<endl;
    }
    int main(){

    map<string,int> m;
    m["One"]=1;
    m["Two"]=2;
    m["Three"]=3;
    map<string,int> ::iterator i1;
    i1= m.begin();
    while(i1!=m.end ()){
    cout << i1.first; // Here is the problem.
    ++i1;
    }
    //foreach(i1.begi n(),i1.end(),pr intall);
    }


  • Andre Kostur

    #2
    Re: map question

    "sam" <sampal@nc.rr.c om> wrote in
    news:gcnNa.2343 56$jp.6169814@t wister.southeas t.rr.com:
    [color=blue]
    > In the following program I am getting an error when I am trying to
    > access i1.first. Is this valid statement?
    > how can I access map value through iterator? Please help me foreach
    > function also.
    >
    >
    > void printall(string str, int i){
    > cout << str<<endl;
    > }
    > int main(){
    >
    > map<string,int> m;
    > m["One"]=1;
    > m["Two"]=2;
    > m["Three"]=3;
    > map<string,int> ::iterator i1;
    > i1= m.begin();
    > while(i1!=m.end ()){
    > cout << i1.first; // Here is the problem.
    > ++i1;
    > }
    > //foreach(i1.begi n(),i1.end(),pr intall);
    > }[/color]

    i1 is an iterator into a map<string, int>, thus it "points" to a pair
    <string, int>. So you'll want:

    cout << i1->first;


    As for your for_each, you'll want it to go from "m.begin()" to "m.end()".
    And printall would need to take a (const?) reference to a pair
    <string,int> in order to have a chance of working. (I unfortunately
    don't use the algorithms often enough to have the details at the top of
    my head....)

    Comment

    • sam

      #3
      Re: map question

      I found the problem.
      I have to access (*i1.first) instead of i1.fist.

      "sam" <sampal@nc.rr.c om> wrote in message
      news:gcnNa.2343 56$jp.6169814@t wister.southeas t.rr.com...[color=blue]
      > In the following program I am getting an error when I am trying to access
      > i1.first. Is this valid statement?
      > how can I access map value through iterator? Please help me foreach
      > function also.
      >
      >
      > void printall(string str, int i){
      > cout << str<<endl;
      > }
      > int main(){
      >
      > map<string,int> m;
      > m["One"]=1;
      > m["Two"]=2;
      > m["Three"]=3;
      > map<string,int> ::iterator i1;
      > i1= m.begin();
      > while(i1!=m.end ()){
      > cout << i1.first; // Here is the problem.
      > ++i1;
      > }
      > //foreach(i1.begi n(),i1.end(),pr intall);
      > }
      >
      >[/color]


      Comment

      • sam

        #4
        Re: map question

        (*i).first is correct.
        "sam" <sampal@nc.rr.c om> wrote in message
        news:oEnNa.2344 65$jp.6175834@t wister.southeas t.rr.com...[color=blue]
        > I found the problem.
        > I have to access (*i1.first) instead of i1.fist.
        >
        > "sam" <sampal@nc.rr.c om> wrote in message
        > news:gcnNa.2343 56$jp.6169814@t wister.southeas t.rr.com...[color=green]
        > > In the following program I am getting an error when I am trying to[/color][/color]
        access[color=blue][color=green]
        > > i1.first. Is this valid statement?
        > > how can I access map value through iterator? Please help me foreach
        > > function also.
        > >
        > >
        > > void printall(string str, int i){
        > > cout << str<<endl;
        > > }
        > > int main(){
        > >
        > > map<string,int> m;
        > > m["One"]=1;
        > > m["Two"]=2;
        > > m["Three"]=3;
        > > map<string,int> ::iterator i1;
        > > i1= m.begin();
        > > while(i1!=m.end ()){
        > > cout << i1.first; // Here is the problem.
        > > ++i1;
        > > }
        > > //foreach(i1.begi n(),i1.end(),pr intall);
        > > }
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Chris \( Val \)

          #5
          Re: map question


          "sam" <sampal@nc.rr.c om> wrote in message
          news:gcnNa.2343 56$jp.6169814@t wister.southeas t.rr.com...
          | In the following program I am getting an error when I am trying to access
          | i1.first. Is this valid statement?
          | how can I access map value through iterator? Please help me foreach
          | function also.
          |
          |
          | void printall(string str, int i){
          | cout << str<<endl;
          | }

          void printall( std::pair<std:: string, int> P ) {
          std::cout << P.first << ", " << P.second << endl;
          }

          | int main(){
          |
          | map<string,int> m;
          | m["One"]=1;
          | m["Two"]=2;
          | m["Three"]=3;
          | map<string,int> ::iterator i1;
          | i1= m.begin();
          | while(i1!=m.end ()){
          | cout << i1.first; // Here is the problem.

          std::cout << i1 -> first;

          | ++i1;
          | }
          | //foreach(i1.begi n(),i1.end(),pr intall);
          | }

          std::for_each( m.begin(), m.end(), printall );

          Cheers.
          Chris Val


          Comment

          • Artie Gold

            #6
            Re: map question

            sam wrote:[color=blue]
            > (*i).first is correct.[/color]

            Which is typically expressed as `i->first' (as per Andre's reply).
            [color=blue]
            > "sam" <sampal@nc.rr.c om> wrote in message
            > news:oEnNa.2344 65$jp.6175834@t wister.southeas t.rr.com...
            >[color=green]
            >>I found the problem.
            >>I have to access (*i1.first) instead of i1.fist.
            >>
            >>"sam" <sampal@nc.rr.c om> wrote in message
            >>news:gcnNa.23 4356$jp.6169814 @twister.southe ast.rr.com...
            >>[color=darkred]
            >>>In the following program I am getting an error when I am trying to[/color]
            >>[/color]
            > access
            >[color=green][color=darkred]
            >>>i1.first. Is this valid statement?
            >>> how can I access map value through iterator? Please help me foreach
            >>>function also.
            >>>
            >>>
            >>>void printall(string str, int i){
            >>> cout << str<<endl;
            >>>}
            >>>int main(){
            >>>
            >>> map<string,int> m;
            >>> m["One"]=1;
            >>> m["Two"]=2;
            >>> m["Three"]=3;
            >>> map<string,int> ::iterator i1;
            >>> i1= m.begin();
            >>> while(i1!=m.end ()){
            >>> cout << i1.first; // Here is the problem.
            >>> ++i1;
            >>> }
            >>> //foreach(i1.begi n(),i1.end(),pr intall);
            >>>}
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]

            HTH,
            --ag

            --
            Artie Gold -- Austin, Texas



            ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
            http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
            ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

            Comment

            Working...