 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tiffie Guest
|
Posted: Sun Jul 23, 2006 1:39 pm Post subject: how to traverse a map |
|
|
hi guys,
imagine i have this map here:
std::map<String,std::map<String,String> >data;
data["bert"]["type"] = "student";
data["jens"]["type"] = "student";
data["mike"]["type"] = "teacher";
data["kim"]["type"] = "teacher";
now i like to traverse the map and show me all the names (bert, jens, ....)
how can i do this in a loop?
Tiffie |
|
| Back to top |
|
 |
Muzaffar Mahkamov Guest
|
Posted: Sun Jul 23, 2006 5:33 pm Post subject: Re: how to traverse a map |
|
|
Tiffie wrote:
| Quote: | hi guys,
imagine i have this map here:
std::map<String,std::map<String,String> >data;
data["bert"]["type"] = "student";
data["jens"]["type"] = "student";
data["mike"]["type"] = "teacher";
data["kim"]["type"] = "teacher";
now i like to traverse the map and show me all the names (bert, jens, ....)
how can i do this in a loop?
Tiffie
|
You should use iterators for map<>. Iter->first will contain the key
element, Iter->second will contain the mapped element:
typedef std::map<String, std::map<String,String> >::iterator
student_iterator_t;
typedef std::map<String,std::map<String,String> > student_list_t;
student_list_t data;
student_iterator_t it = data.begin();
student_iterator_t end = data.end();
while(it != end)
{
std::cout << it->first << std::endl;
++it;
} |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|