By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,218 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,218 IT Pros & Developers. It's quick & easy.

stl hash_map and user defined data type update

P: n/a
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 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
an************@gmail.com 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

Jun 2 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.