Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map iterator

Reply
Thread Tools

map iterator

 
 
thomas
Guest
Posts: n/a
 
      07-06-2008
a question about map iterator

---code---
#include<map>
#include<iterator>
using namespace std;

int main(){
map<string,int> mp;
mp.insert(pair<string,int>("hello",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?
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      07-06-2008
"thomas" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      07-08-2008
"thomas" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>a question about map iterator
>
> ---code---
> #include<map>
> #include<iterator>
> using namespace std;
>
> int main(){
> map<string,int> mp;
> mp.insert(pair<string,int>("hello",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;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
will an iterator to a map becomes invalid when an element is inserted into the map wolverine C++ 3 07-31-2006 12:24 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments