Can I use user defined data types in maps?

Discussion in 'C++' started by Anamika, Oct 12, 2006.

  1. Anamika

    Anamika Guest

    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...
     
    Anamika, Oct 12, 2006
    #1
    1. Advertising

  2. 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
     
    Thomas Tutone, Oct 12, 2006
    #2
    1. Advertising

  3. Anamika

    Daniel T. Guest

    "Anamika" <> 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.
     
    Daniel T., Oct 12, 2006
    #3
  4. Anamika

    Jim Langston Guest

    "Anamika" <> wrote in message
    news:...
    >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.
     
    Jim Langston, Oct 13, 2006
    #4
  5. Anamika

    Marcus Kwok Guest

    Jim Langston <> wrote:
    > "Anamika" <> wrote in message
    > news:...
    >>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
     
    Marcus Kwok, Oct 17, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Elliott
    Replies:
    4
    Views:
    1,173
    Simon Elliott
    Mar 10, 2005
  2. Oodini
    Replies:
    1
    Views:
    1,795
    Keith Thompson
    Sep 27, 2005
  3. ramu
    Replies:
    2
    Views:
    332
    rlblaster
    Feb 20, 2006
  4. Marcus
    Replies:
    2
    Views:
    598
    Marcus
    Dec 9, 2005
  5. Gary Roach
    Replies:
    0
    Views:
    116
    Gary Roach
    Sep 1, 2013
Loading...

Share This Page