Modifying elements of hash_set.

Discussion in 'C++' started by Amit Bhatia, Mar 5, 2007.

  1. Amit Bhatia

    Amit Bhatia Guest

    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.
    Amit Bhatia, Mar 5, 2007
    #1
    1. Advertising

  2. On Mar 5, 5:51 pm, Amit Bhatia <> wrote:

    > 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
    Thomas Tutone, Mar 6, 2007
    #2
    1. Advertising

  3. Amit Bhatia

    P.J. Plauger Guest

    "Thomas Tutone" <> wrote in message
    news:...

    > On Mar 5, 5:51 pm, Amit Bhatia <> wrote:
    >
    >> 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.


    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
    P.J. Plauger, Mar 6, 2007
    #3
  4. Amit Bhatia

    Amit Bhatia Guest

    P.J. Plauger wrote:
    > "Thomas Tutone" <> wrote in message
    > news:...
    >
    >> On Mar 5, 5:51 pm, Amit Bhatia <> wrote:
    >>
    >>> 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.

    >
    > 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.
    Amit Bhatia, Mar 6, 2007
    #4
  5. Amit Bhatia

    mimi Guest

    On 3ÔÂ6ÈÕ, ÏÂÎç1ʱ22·Ö, Amit Bhatia <abha...@nospam..nospam.com> wrote:
    > P.J. Plauger wrote:
    > > "Thomas Tutone" <> wrote in message
    > >news:...

    >
    > >> On Mar 5, 5:51 pm, Amit Bhatia <> wrote:

    >
    > >>> 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.

    >
    > > 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.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -

    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.
    mimi, Mar 6, 2007
    #5
  6. Amit Bhatia

    P.J. Plauger Guest

    "mimi" <> wrote in message
    news:...

    On 3ÔÂ6ÈÕ, ÏÂÎç1ʱ22·Ö, Amit Bhatia <> wrote:
    > P.J. Plauger wrote:
    > > "Thomas Tutone" <> wrote in message
    > >news:...

    >
    > >> On Mar 5, 5:51 pm, Amit Bhatia <> wrote:

    >
    > >>> 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.

    >
    > > 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.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -

    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
    P.J. Plauger, Mar 6, 2007
    #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. Vasileios
    Replies:
    4
    Views:
    467
    Rolf Magnus
    Nov 4, 2003
  2. Abhijit Ray
    Replies:
    1
    Views:
    588
    John Harrison
    Apr 27, 2004
  3. Alex Gerdemann

    Performance of hash_set vs. Java

    Alex Gerdemann, Oct 11, 2004, in forum: C++
    Replies:
    10
    Views:
    2,253
    Tom Widmer
    Oct 13, 2004
  4. Amit Bhatia

    Modifying elements of hash_set.

    Amit Bhatia, Mar 5, 2007, in forum: C++
    Replies:
    0
    Views:
    336
    Amit Bhatia
    Mar 5, 2007
  5. eric
    Replies:
    3
    Views:
    1,291
    Michael DOUBEZ
    Jul 12, 2011
Loading...

Share This Page