what datatypes of key can be used in c++ maps?

Discussion in 'C++' started by aparna, Feb 9, 2006.

  1. aparna

    aparna Guest

    I want to used datastructure in which key value i want to use is
    String pointer & associated with each key there is one dimensional
    integer array which i want dynamic because size of integer array may be
    variable .So can anyone suggest me can I use maps or what datastructure
    will be suitable?
    Aparna :)
     
    aparna, Feb 9, 2006
    #1
    1. Advertising

  2. * aparna:
    > I want to used datastructure in which key value i want to use is
    > String pointer & associated with each key there is one dimensional
    > integer array which i want dynamic because size of integer array may be
    > variable .So can anyone suggest me can I use maps or what datastructure
    > will be suitable?


    std::map< std::string, std::vector<int> >

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 9, 2006
    #2
    1. Advertising

  3. aparna

    Guest

    Re: what datatypes of key can be used in c++ maps?

    map<string, int*> may be suitable. Or you can wrap int* in a smart
    pointer. Just note that if you use string* as the key, that means you
    are using address of a string as the key instead of the actual content
    of the string.
     
    , Feb 9, 2006
    #3
  4. aparna

    Sumit Rajan Guest

    "aparna" <> wrote in message
    news:...
    >I want to used datastructure in which key value i want to use is
    > String pointer & associated with each key there is one dimensional
    > integer array which i want dynamic because size of integer array may be
    > variable .So can anyone suggest me can I use maps or what datastructure
    > will be suitable?


    Why don't you use a std::map<std::string, std::vector<int> >?

    Regards,
    Sumit.
     
    Sumit Rajan, Feb 9, 2006
    #4
  5. aparna

    Guest

    Re: what datatypes of key can be used in c++ maps?

    wrote:
    > map<string, int*> may be suitable. Or you can wrap int* in a smart
    > pointer. Just note that if you use string* as the key, that means you
    > are using address of a string as the key instead of the actual content
    > of the string.


    Or better yet, as other people have mentioned, use map<string,
    vector<int> >
     
    , Feb 9, 2006
    #5
  6. aparna

    Guest

    Re: what datatypes of key can be used in c++ maps?

    The way I read it you want to a map<std::string*,std::vector<int> >. (A
    reference to a string pointer)
    This is also fine but you need to think a bit about what you are doing.

    In effect this maps a vector to the memory address where your string
    object resides.

    Be sure to declare a comparator if you want to sort your map.
    (
    something like:
    class CompareStringPointers
    {
    bool operator()(const std::string* A,const std::string* B ) const
    {return *A<*B;}
    };
    )
     
    , Feb 9, 2006
    #6
    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,180
    Simon Elliott
    Mar 10, 2005
  2. Marcus
    Replies:
    2
    Views:
    611
    Marcus
    Dec 9, 2005
  3. mygulamali
    Replies:
    5
    Views:
    438
    dasjotre
    Jun 19, 2007
  4. Casey Hawthorne
    Replies:
    1
    Views:
    745
    Arne Vajhøj
    Mar 18, 2009
  5. M P
    Replies:
    1
    Views:
    523
Loading...

Share This Page