Can I use user defined data types in maps?

A

Anamika

I want to create a map,which will be having string as a key and the
related data as a structure...

So can I use user defined data types as the data in maps? If so How I
can use it...
What is the syntax of that?...
PLease tell me that....
Thanks for answering me...
 
T

Thomas Tutone

Anamika said:
I want to create a map,which will be having string as a key and the
related data as a structure...
OK.

So can I use user defined data types as the data in maps?
Yes.

If so How I
can use it...
What is the syntax of that?...

#include <string>
#include <map>

class Anamika { // whatever
};

std::map<std::string, Anamika> anamikaMap;

Best regards,

Tom
 
D

Daniel T.

Anamika said:
I want to create a map,which will be having string as a key and the
related data as a structure...

So can I use user defined data types as the data in maps? If so How I
can use it...
What is the syntax of that?...
PLease tell me that....
Thanks for answering me...

struct MyType { };

std::map<string, MyType>
 
J

Jim Langston

Anamika said:
I want to create a map,which will be having string as a key and the
related data as a structure...

So can I use user defined data types as the data in maps?
Yes.
If so How I can use it...

Same way you do with built in variables.

std::map<int, MyClass> MyMap;

Now you can:
MyMap[0] = MyClass(); // Insert default constructed instance
or
MyClass MyInstance;
MyMap[0] = MyInstance; // Copy previously declared instance
etc..
It all follows the regular std::map syntax (.MyMap.insert(
std::makepair said:
What is the syntax of that?...

Same as with normal maps. Is it the regular map syntax you need? Or what?
I suggest you google for std::map c++ and read up on the hits to learn how
to use maps if you haven't yet.
PLease tell me that....
Okay.

Thanks for answering me...

You're welcome.
 
M

Marcus Kwok

Jim Langston said:
Anamika said:
I want to create a map,which will be having string as a key and the
related data as a structure...

So can I use user defined data types as the data in maps?
Yes.
If so How I can use it...

Same way you do with built in variables.

std::map<int, MyClass> MyMap;

Now you can:
MyMap[0] = MyClass(); // Insert default constructed instance
or
MyClass MyInstance;
MyMap[0] = MyInstance; // Copy previously declared instance
etc..

Of course, the behavior of operator[] on a map will insert a
default-constructed instance upon first access to an element, so if
MyMap[0] doesn't already exist, then just doing

MyMap[0];

should create the entry.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top