By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

trying to get the second 2nd value of same key in multimap

P: 14
I am unable to get the "is" I keep getting the below:

the find : this
the find : this


what I want is:

the find : this
the find : is


from the below code:
Expand|Select|Wrap|Line Numbers
  1. typedef multimap<int,string> IntStringMMap;
  2.  
  3. IntStringMMap coll; // container for int/string values
  4.  
  5. coll.insert(make_pair(2,"a"));
  6. coll.insert(make_pair(1,"this"));
  7. coll.insert(make_pair(4,"of"));
  8. coll.insert(make_pair(1,"is"));
  9. coll.insert(make_pair(3,"multimap"));
  10.  
  11.  
  12. IntStringMMap::iterator post = coll.find(1);
  13. cout << "the find : " << post->second << endl;
  14. post = coll.find(1);
  15. cout << "the find : " << post->second << endl;
Nov 1 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Try

Expand|Select|Wrap|Line Numbers
  1. IntStringMMap::iterator post = coll.find(1);
  2. cout << "the find : " << post->second << endl;
  3. post++;
  4. cout << "the find : " << post->second << endl;
  5.  
Nov 1 '06 #2

P: 14
[quote=Banfa]

Thanks very much Banfa.
Just one query. Is this "post++" (i.e. next same-key value is at next data-type + 1 address location) a compiler/c++ standard or it varies on
windows or other unix platforms?

On the other hand your solution made me to dump another workaround.
It works great.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.     // type of the collection
  10.     typedef multimap<int,string> IntStringMMap;
  11.  
  12.     IntStringMMap coll;        // container for int/string values
  13.  
  14.     // insert some elements in arbitrary order
  15.     // - a value with key 1 gets inserted twice
  16.     coll.insert(make_pair(5,"tagged"));
  17.     coll.insert(make_pair(2,"a"));
  18.     coll.insert(make_pair(1,"this"));
  19.     coll.insert(make_pair(4,"of"));
  20.     coll.insert(make_pair(6,"strings"));
  21.     coll.insert(make_pair(1,"is"));
  22.     coll.insert(make_pair(3,"multimap"));
  23.  
  24.    // Find on srchkey and print all values on the srchkey
  25.    int srchkey = 1;
  26.    IntStringMMap::iterator post = coll.find(srchkey);
  27.    while( post != coll.end() && post->first == srchkey )
  28.    {
  29.       cout << "key value " << srchkey << " has : "  << post->second << endl;
  30.       post++;
  31.    }
  32. }
  33.  
Nov 6 '06 #3

Post your reply

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