map iterator

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

    map iterator

    a question about map iterator

    ---code---
    #include<map>
    #include<iterat or>
    using namespace std;

    int main(){
    map<string,int mp;
    mp.insert(pair< string,int>("he llo",1));
    for(map<string, int>::iterator it(mp.begin()); it<mp.end(); it+
    +) ///----L---
    cout<<it->first<<endl;
    }
    ----code ----

    it cannot compile; if I change the "it<mp.end( )" to "it!=mp.end ()"
    in line L , it works.

    for int vectors, I can use "it<vi.end( )" to make boundary check, but
    for map it doesn't works.

    any explaination or suggestions?
  • Andrew Koenig

    #2
    Re: map iterator

    "thomas" <FreshThomas@gm ail.comwrote in message
    news:5a901f5a-4d58-4e95-9cff-a3d316ecfa81@y3 8g2000hsy.googl egroups.com...
    for int vectors, I can use "it<vi.end( )" to make boundary check, but
    for map it doesn't works.
    any explaination or suggestions?
    Vector iterators are random-access iterators. Map iterators aren't. If
    this doesn't answer your question completely, please study the definition of
    random-access iterators carefully.


    Comment

    • Daniel T.

      #3
      Re: map iterator

      "Andrew Koenig" <ark@acm.orgwro te:
      "thomas" <FreshThomas@gm ail.comwrote:
      >
      for int vectors, I can use "it<vi.end( )" to make boundary check, but
      for map it doesn't works.

      any explaination or suggestions?
      >
      Vector iterators are random-access iterators. Map iterators aren't. If
      this doesn't answer your question completely, please study the definition of
      random-access iterators carefully.
      It might also help to know that map's iterator is a bidirectional
      iterator. Studying the difference between random-access iterators and
      bidirectional iterators might be helpful.

      Comment

      • Jim Langston

        #4
        Re: map iterator

        "thomas" <FreshThomas@gm ail.comwrote in message
        news:5a901f5a-4d58-4e95-9cff-a3d316ecfa81@y3 8g2000hsy.googl egroups.com...
        >a question about map iterator
        >
        ---code---
        #include<map>
        #include<iterat or>
        using namespace std;
        >
        int main(){
        map<string,int mp;
        mp.insert(pair< string,int>("he llo",1));
        for(map<string, int>::iterator it(mp.begin()); it<mp.end(); it+
        +) ///----L---
        cout<<it->first<<endl;
        }
        ----code ----
        >
        it cannot compile; if I change the "it<mp.end( )" to "it!=mp.end ()"
        in line L , it works.
        >
        for int vectors, I can use "it<vi.end( )" to make boundary check, but
        for map it doesn't works.
        >
        any explaination or suggestions?
        You've already gotten the explanation. The suggestion is instead of using <
        use !=

        for(map<string, int>::iterator it(mp.begin()); it != mp.end(); ++it) {
        cout<<it->first<<endl;
        }


        Comment

        Working...