Find specific element of multimap

F

francois.chartrand

Hi,

I would like to know if there is a way to locate a specific pair in a
multimap ?

Example:

std::multimap<int,int> myMap;
std::multimap<int,int>::iterator myMapIterator;

myMap.insert(std::make_pair(1,10));
myMap.insert(std::make_pair(1,11));
myMap.insert(std::make_pair(1,12));
myMap.insert(std::make_pair(2,10));
myMap.insert(std::make_pair(2,11));
myMap.insert(std::make_pair(2,12));

myMapIterator myIterator = myMap.find(std::make_pair(2,10));
 
P

Pete Becker

I would like to know if there is a way to locate a specific pair in a
multimap ?

Not in general. multimap is designed for looking up keys, not keys and
values. You might be better off with a multiset, and various comparison
functions that look at either what is currently the key value or at the
entire element.
 
M

Mark P

Hi,

I would like to know if there is a way to locate a specific pair in a
multimap ?

Example:

std::multimap<int,int> myMap;
std::multimap<int,int>::iterator myMapIterator;

myMap.insert(std::make_pair(1,10));
myMap.insert(std::make_pair(1,11));
myMap.insert(std::make_pair(1,12));
myMap.insert(std::make_pair(2,10));
myMap.insert(std::make_pair(2,11));
myMap.insert(std::make_pair(2,12));

myMapIterator myIterator = myMap.find(std::make_pair(2,10));
On top of what Pete said, you can use lower_bound and upper_bound with
the desired key and then iterate over the range [LB,UB) to see if any of
the values match. (Or just iterate up from LB and define your own
stopping condition, which may be more efficient.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top