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:

typedef multimap<int,string> IntStringMMap;

IntStringMMap coll; // container for int/string values

coll.insert(make_pair(2,"a"));
coll.insert(make_pair(1,"this"));
coll.insert(make_pair(4,"of"));
coll.insert(make_pair(1,"is"));
coll.insert(make_pair(3,"multimap"));


IntStringMMap::iterator post = coll.find(1);
cout << "the find : " << post->second << endl;
post = coll.find(1);
cout << "the find : " << post->second << endl;

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:

typedef multimap<int,string> IntStringMMap;

IntStringMMap coll; // container for int/string values

coll.insert(make_pair(2,"a"));
coll.insert(make_pair(1,"this"));
coll.insert(make_pair(4,"of"));
coll.insert(make_pair(1,"is"));
coll.insert(make_pair(3,"multimap"));


IntStringMMap::iterator post = coll.find(1);
cout << "the find : " << post->second << endl;
post = coll.find(1);
cout << "the find : " << post->second << endl;

Hmm, have you looked at the documentation for std::map perhaps?

multimap::find returns only the first match. So calling it twice will not make any difference. You will have to loop through the map and see if the key is equal to the value you are looking for.
Something like this perhaps.

size_t no_of_items = coll.count(1);
    size_t matches_found =0 ;
    IntStringMMap::iterator itr;
    for (itr = coll.begin(); itr != coll.end() && matches_found < no_of_items ; ++itr) 
    {
        if (itr->first == 1) 
        {
            cout << "the find : " << itr->second << endl;
            ++matches_found;
        }
    }

std::map doesnt allow duplicate keys.
Another easy way to implement the solution is to use equal_range to get the start and end positions of elements with matching key value, and iterate between them, to locate all the elements.
Also there may be some functions that you can use in the stl::algorithm library.

This article has been dead for over six months. Start a new discussion instead.