Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl hash_map and user defined data type update

Reply
Thread Tools

stl hash_map and user defined data type update

 
 
anjangoswami06@gmail.com
Guest
Posts: n/a
 
      06-02-2006
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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      06-02-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/UniqueAs...ontainer.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

 
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
STL sort with user defined data type mweltin@gmail.com C++ 3 07-17-2007 01:59 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
[STL] hash_map problem gibffe C++ 2 10-30-2004 06:38 AM
STL hash_map and free/delete the key object Arun Dharankar C++ 3 06-03-2004 07:30 PM
STL hash_map hash Murali C++ 3 02-09-2004 06:42 AM



Advertisments