Topic: STL map and multimap question.
Author: herwin@gmu.edu (Harry Erwin)
Date: 1997/12/03 Raw View
Bill Wade <bill.wade@stoner.com> wrote:
> SB <seenu@ix.removethis.netcom.com> wrote in article
> <347500C9.4D11F262@ix.removethis.netcom.com>...
> > Hello, this question pertains to both maps and multimaps.
> >
> > Is there anyway a Key for map or multimap element can be
> > changed without having to remove and re-insert the element
> > with the new Key?
>
> In general no. In general elements don't know what map they belong to, so
> they have no way of telling the map they were changed. Maps promise to
> find an element in O(log(n)) time. If all N elements have changed without
> the map being told, its hard to imagine how a find can occur in less than
> O(n) time.
>
> I suspect that most existing map implementations "won't mind" if a key
> changes in a way that doesn't change the current order, but there is no
> promise of even that much.
I was recently checking out Metrowerks Codewarrior in the corresponding
area for sets and discovered that it did care... If you want to write
transportable code, insert the value with the new key and delete the
old.
Cheers,
Harry Erwin
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/11/21 Raw View
SB <seenu@ix.removethis.netcom.com> writes:
>
> Hello, this question pertains to both maps and multimaps.
>
> Is there anyway a Key for map or multimap element can be
> changed without having to remove and re-insert the element
> with the new Key?
No.
That's deliberate, and essential: in general, changing an
element's key changes its position. (Maps and multimaps are
always sorted in ascending order by key.)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/11/24 Raw View
SB <seenu@ix.removethis.netcom.com> wrote in article
<347500C9.4D11F262@ix.removethis.netcom.com>...
> Hello, this question pertains to both maps and multimaps.
>
> Is there anyway a Key for map or multimap element can be
> changed without having to remove and re-insert the element
> with the new Key?
In general no. In general elements don't know what map they belong to, so
they have no way of telling the map they were changed. Maps promise to
find an element in O(log(n)) time. If all N elements have changed without
the map being told, its hard to imagine how a find can occur in less than
O(n) time.
I suspect that most existing map implementations "won't mind" if a key
changes in a way that doesn't change the current order, but there is no
promise of even that much.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: SB <seenu@ix.removethis.netcom.com>
Date: 1997/11/21 Raw View
Hello, this question pertains to both maps and multimaps.
Is there anyway a Key for map or multimap element can be
changed without having to remove and re-insert the element
with the new Key?
For instance, the map I use maintains a list of objects and its
Key is an attribute (guaranteed to be unique) from within those
objects. From time to time, I need to find elements from this
map and change that attribute that is used as the Key (that
attribute is still guaranteed to be unique after the change). I can
change the attribute value within the object once I find the
element in question, but is there anyway I can change the Key
associated with that object without having to remove that
element and re-insert reflecting the new Key value?
Any help is grately appreciated.
Thanks a bunch.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]