do...while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metalinc
    New Member
    • Jan 2007
    • 14

    #1

    do...while loop

    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??
    Code:
    do
    {
      if(mymap.find(input)!= mymap.end())
      {
          cout<< mymap.find(input)->second<<endl;
          mymap.find(input)++;
      }
      else
         cout<<"No word(s) found."<<endl;
    }
      while(mymap.find(input)!= mymap.end()); //this cause endless loop
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by metalinc
    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??
    Code:
    do
    {
      if(mymap.find(input)!= mymap.end())
      {
          cout<< mymap.find(input)->second<<endl;
          mymap.find(input)++;
      }
      else
         cout<<"No word(s) found."<<endl;
    }
      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

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Are you using a multimap? The answer must be yes or you can't have duplicate keys.

      Assuming that, your code should look like:

      [code=cpp]
      pair< multimap< etc>::iterator, multimap< etc>::iterator > range;

      range = equal_range(mym ap.begin(), mymap.end(), input);

      multimap< etc>::iterator itr = range.first;

      while (itr != range.second)
      {
      //cout your data
      itr++;
      }
      [/code]

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

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by weaknessforcats
        Are you using a multimap? The answer must be yes or you can't have duplicate keys.

        Assuming that, your code should look like:

        [code=cpp]
        pair< multimap< etc>::iterator, multimap< etc>::iterator > range;

        range = equal_range(mym ap.begin(), mymap.end(), input);

        multimap< etc>::iterator itr = range.first;

        while (itr != range.second)
        {
        //cout your data
        itr++;
        }
        [/code]

        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

        Comment

        Working...