Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I use user defined data types in maps?

Reply
Thread Tools

Can I use user defined data types in maps?

 
 
Anamika
Guest
Posts: n/a
 
      10-12-2006
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...

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      10-12-2006

Anamika wrote:
> 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

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-12-2006
"Anamika" <(E-Mail Removed)> wrote:

> 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>

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-13-2006
"Anamika" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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<int, MyClass>( 10, MyMap() ) ); etc...

> 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.


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      10-17-2006
Jim Langston <(E-Mail Removed)> wrote:
> "Anamika" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
stl ostream_iterators with user defined types grahamo C++ 4 02-23-2004 03:37 AM
Doubt in distinguishing between modules and user-defined types main features. Guilherme Pinto C++ 1 10-10-2003 04:30 PM



Advertisments