Multimap - retrieve when the value is not a primitive type

A

ambarish.mitra

Hi all,

I have a multimap, where key is an int and the value is a class. I can
insert into the multimap, but finding it difficult to retrieve the
value when keys match. I can do this with primitive types (int/char
etc).

Example:

class A {
public:
int vol;
int temp;
};

multimap<int, A> mechanics;
multimap<int, A>::iterator it;

A aobj;
aobj.vol=10;
aobj.temp=20;

mechanics.insert(pair<int, A>(5, aobj));
// many insert statements here....


std::pair<multimap<int,A>::iterator,multimap<int,A>::iterator> ret;

ret = mechanics.equal_range(5);

for (it=ret.first; it!=ret.second; ++it)
// How to read the object that matches the key in the multimap?
;


Upto this is okay, but then how can I find out the object
corresponding to the given key in the multimap?

Any help/pointers?

Regards.
 
K

Kai-Uwe Bux

Hi all,

I have a multimap, where key is an int and the value is a class. I can
insert into the multimap, but finding it difficult to retrieve the
value when keys match. I can do this with primitive types (int/char
etc).

Strange. There is no difference when it comes to class types.

Example:

class A {
public:
int vol;
int temp;
};

multimap<int, A> mechanics;
multimap<int, A>::iterator it;

A aobj;
aobj.vol=10;
aobj.temp=20;

mechanics.insert(pair<int, A>(5, aobj));
// many insert statements here....


std::pair<multimap<int,A>::iterator,multimap<int,A>::iterator> ret;

ret = mechanics.equal_range(5);

for (it=ret.first; it!=ret.second; ++it)
// How to read the object that matches the key in the multimap?

Did you try

it->second

;
[snip]


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top