454,377 Members | 1,660 Online Need help? Post your question and get tips & solutions from a community of 454,377 IT Pros & Developers. It's quick & easy.

# do...while loop

 P: 14 i'm trying to do a do while loop, to keep printing the data that matches the key of mymap. my while statement printed an endless loop.... What should the while statement be?? Expand|Select|Wrap|Line Numbers do {   if(mymap.find(input)!= mymap.end())   {       cout<< mymap.find(input)->second<
3 Replies

 Expert 100+ P: 1,251 i'm trying to do a do while loop, to keep printing the data that matches the key of mymap. my while statement printed an endless loop.... What should the while statement be?? Expand|Select|Wrap|Line Numbers do {   if(mymap.find(input)!= mymap.end())   {       cout<< mymap.find(input)->second<

 Expert Mod 5K+ P: 9,197 Are you using a multimap? The answer must be yes or you can't have duplicate keys. Assuming that, your code should look like: Expand|Select|Wrap|Line Numbers pair< multimap< etc>::iterator, multimap< etc>::iterator >  range;   range = equal_range(mymap.begin(), mymap.end(), input);   multimap< etc>::iterator  itr = range.first;   while (itr != range.second) {      //cout your data      itr++; }   I didn't compile this. This is just to give you the idea. equal_range is an STL algorithm that return a pair of iterators that bracket the range of elments in the constainer that have the specified value. The first iterator you an use as a begin() and the second iterator you can use an end(). May 31 '07 #3

 Expert 100+ P: 1,251 Are you using a multimap? The answer must be yes or you can't have duplicate keys. Assuming that, your code should look like: Expand|Select|Wrap|Line Numbers pair< multimap< etc>::iterator, multimap< etc>::iterator >  range;   range = equal_range(mymap.begin(), mymap.end(), input);   multimap< etc>::iterator  itr = range.first;   while (itr != range.second) {      //cout your data      itr++; }   I didn't compile this. This is just to give you the idea. equal_range is an STL algorithm that return a pair of iterators that bracket the range of elments in the constainer that have the specified value. The first iterator you an use as a begin() and the second iterator you can use an end(). Cool, good to know. ;) Adrian May 31 '07 #4 