stl hash_map and user defined data type update

Discussion in 'C++' started by anjangoswami06@gmail.com, Jun 2, 2006.

  1. Guest

    Hi,

    I am interested to know how it is possible to update the data type with
    "insert" with stl hash_map. Suppose we have,
    hash_map<const char *, MyDataType, hash_compare<const char*,
    some_compare_func_obj>>
    x;

    //I want to insert a new instance of MyDataType if a new key is
    encountered but if it is a key //which already exists I want to update
    the existing values of the members of MyDataType //instance which is
    associated with the key.

    //MyDataType may be as follows:

    struct MyDataType {
    int x;
    int y;
    char[2048] str;
    MyDataType(){x=0;y=0; strcpy(str,"NONEYET"));
    };

    //one way of insertion is the following but I will not be able to
    update already instanciated
    //MyDataType in a straight manner.
    x.insert(make_pair("some string",MyDataType data))

    //I am trying to find a simple and elegant method for this.

    I will appriciate any suggestion.

    Thanks.
    Buchan
     
    , Jun 2, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    >
    > I am interested to know how it is possible to update the data type with
    > "insert" with stl hash_map. Suppose we have,
    > hash_map<const char *, MyDataType, hash_compare<const char*,
    > some_compare_func_obj>>
    > x;
    >
    > //I want to insert a new instance of MyDataType if a new key is
    > encountered but if it is a key //which already exists I want to update
    > the existing values of the members of MyDataType //instance which is
    > associated with the key.
    >
    > //MyDataType may be as follows:
    >
    > struct MyDataType {
    > int x;
    > int y;
    > char[2048] str;
    > MyDataType(){x=0;y=0; strcpy(str,"NONEYET"));
    > };
    >
    > //one way of insertion is the following but I will not be able to
    > update already instanciated
    > //MyDataType in a straight manner.
    > x.insert(make_pair("some string",MyDataType data))
    >
    > //I am trying to find a simple and elegant method for this.
    >
    > I will appriciate any suggestion.


    Although you may not know or care, your question is on-topic here
    because hash_map is part of TR1 (as std::tr1::unordered_map).

    Conceptually, a hash_map is a Unique Associative Container (see
    http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html), which
    means it doesn't allow non-unique keys. If you supply a non-unique key,
    hash_map::insert() doesn't replace the existing one but rather returns
    an iterator to the existing key/value. Thus, to force a replacement of
    an existing value, you can do this:

    (*((x.insert(make_pair("some string", MyDataType()))).first)).second =
    data;

    where data is of MyDataType. Pretty ugly, huh? That's why they also
    supply a convenience function that allows you to do the same thing like
    this:

    x[ "some string" ] = data;

    Elegant enough!

    Cheers! --M
     
    mlimber, Jun 2, 2006
    #2
    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. Murali

    STL hash_map hash

    Murali, Feb 8, 2004, in forum: C++
    Replies:
    3
    Views:
    12,115
    John Harrison
    Feb 9, 2004
  2. Arun Dharankar
    Replies:
    3
    Views:
    8,748
    Arun Dharankar
    Jun 3, 2004
  3. gibffe

    [STL] hash_map problem

    gibffe, Oct 29, 2004, in forum: C++
    Replies:
    2
    Views:
    2,837
    John Harrison
    Oct 30, 2004
  4. Oodini
    Replies:
    1
    Views:
    1,838
    Keith Thompson
    Sep 27, 2005
  5. Replies:
    3
    Views:
    528
    Kai-Uwe Bux
    Jul 17, 2007
Loading...

Share This Page