By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
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
  1. do
  2. {
  3.   if(mymap.find(input)!= mymap.end())
  4.   {
  5.       cout<< mymap.find(input)->second<<endl;
  6.       mymap.find(input)++;
  7.   }
  8.   else
  9.      cout<<"No word(s) found."<<endl;
  10. }
  11.   while(mymap.find(input)!= mymap.end()); //this cause endless loop
May 30 '07 #1
Share this Question
Share on Google+
3 Replies


AdrianH
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
  1. do
  2. {
  3.   if(mymap.find(input)!= mymap.end())
  4.   {
  5.       cout<< mymap.find(input)->second<<endl;
  6.       mymap.find(input)++;
  7.   }
  8.   else
  9.      cout<<"No word(s) found."<<endl;
  10. }
  11.   while(mymap.find(input)!= mymap.end()); //this cause endless loop
A map is a way of mapping a key to an item. That is a 1:1 mapping. Find, finds that item using the key.

See here for more details.

Perhaps you want a map of a key to a list or other container?


Adrian
May 31 '07 #2

weaknessforcats
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
  1. pair< multimap< etc>::iterator, multimap< etc>::iterator >  range;
  2.  
  3. range = equal_range(mymap.begin(), mymap.end(), input);
  4.  
  5. multimap< etc>::iterator  itr = range.first;
  6.  
  7. while (itr != range.second)
  8. {
  9.      //cout your data
  10.      itr++;
  11. }
  12.  
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

AdrianH
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
  1. pair< multimap< etc>::iterator, multimap< etc>::iterator >  range;
  2.  
  3. range = equal_range(mymap.begin(), mymap.end(), input);
  4.  
  5. multimap< etc>::iterator  itr = range.first;
  6.  
  7. while (itr != range.second)
  8. {
  9.      //cout your data
  10.      itr++;
  11. }
  12.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.