Modifying elements of hash_set.

A

Amit Bhatia

Hi,
I was wondering if I have a hash_set, can I modify its elements using an
iterator: given the fact that the changes I make will not change the
position or the key of the object that the iterator points to?

Or do I need to necessarily use hash_map ?

Thanks,
--a.
 
T

Thomas Tutone

I was wondering if I have a hash_set, can I modify its elements using an
iterator: given the fact that the changes I make will not change the
position or the key of the object that the iterator points to?

Or do I need to necessarily use hash_map ?

Standard C++ doesn't have anything called a hash_set or hash_map -
those are extensions that some implementations have added. The next
version of C++ will have what is now designated as
std::tr1::unordered_set and std::tr1::unordered_map, which fill a
similar purpose, but have slightly different syntax. For more
information about unordered_set and unordered_map, take a look at
section 6.3 of this:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

Or take a look at Pete Becker's new book on tr1.

Best regards,

Tom
 
P

P.J. Plauger

Standard C++ doesn't have anything called a hash_set or hash_map -
those are extensions that some implementations have added. The next
version of C++ will have what is now designated as
std::tr1::unordered_set and std::tr1::unordered_map, which fill a
similar purpose, but have slightly different syntax. For more
information about unordered_set and unordered_map, take a look at
section 6.3 of this:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf

Or take a look at Pete Becker's new book on tr1.

That's the officious answer. The useful answer is that hash_set
is just like (unordered_)set in this regard -- if you do contrive to
alter the stored key you risk destroying the integrity of the
container. Erase the old value and insert the new value instead.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
A

Amit Bhatia

P.J. Plauger said:
That's the officious answer. The useful answer is that hash_set
is just like (unordered_)set in this regard -- if you do contrive to
alter the stored key you risk destroying the integrity of the
container. Erase the old value and insert the new value instead.

Thanks. But let's say each object in the hash_set has a boolean, and a
vector of integers, whose value(s) do not affect the key for sure (I
supply the hash function): then won't this method of copying-erasing-
changing-inserting result in substantial speed slow up? I just might
want to change boolean variable in the class object and still I have to
copy the whole thing first..

thanks,
--a.
 
M

mimi

Thanks. But let's say each object in the hash_set has a boolean, and a
vector of integers, whose value(s) do not affect the key for sure (I
supply the hash function): then won't this method of copying-erasing-
changing-inserting result in substantial speed slow up? I just might
want to change boolean variable in the class object and still I have to
copy the whole thing first..

thanks,
--a.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
Maybe you could take the boolean value out and associate it with with
each object by using hash_map.
Or you could just use const_cast, which is evil.
 
P

P.J. Plauger

Thanks. But let's say each object in the hash_set has a boolean, and a
vector of integers, whose value(s) do not affect the key for sure (I
supply the hash function): then won't this method of copying-erasing-
changing-inserting result in substantial speed slow up? I just might
want to change boolean variable in the class object and still I have to
copy the whole thing first..

thanks,
--a.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
Maybe you could take the boolean value out and associate it with with
each object by using hash_map.

[pjp] Right. If your "key" really has components that don't participate in
the
ordering, then you really have a map masquerading as a set. And if
on top of that the non-ordering components are heavyweight, then
you're really being perverse in not separating them out as the
"mapped" part of a (key, mapped) pair stored in a map.

Or you could just use const_cast, which is evil.

[pjp] Indeed.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top