When deleting elements in a map is the ordering preserved. For
example say I have elements A, B and C (order A, B, C) in a map and B
is deleted, is the resulting ordering always A, C.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
danwgrace (24)
|
6/25/2008 4:59:21 PM |
|
Daniel wrote:
> When deleting elements in a map is the ordering preserved. For
> example say I have elements A, B and C (order A, B, C) in a map and B
> is deleted, is the resulting ordering always A, C.
A map maintains its internal ordering by comparing the keys, and it is
required roughly that if A<B and B<C then A<C. Therefore, deleting an
element does not affect the others' ordering.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
6/26/2008 2:23:03 AM
|
|
On Jun 26, 4:59 am, Daniel <danwgr...@gmail.com> wrote:
> When deleting elements in a map is the ordering preserved. For
> example say I have elements A, B and C (order A, B, C) in a map and B
> is deleted, is the resulting ordering always A, C.
If we are talking about std::map, which are typically implemented as
trees, then the ordering is preserved if you access these by
iterators. This will not be the case for std::tr1::unordered_map
though.
Please correct me if I'm mistaken.
Best,
Sarang
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Sarang
|
6/26/2008 2:44:40 AM
|
|
On Jun 26, 4:59 am, Daniel <danwgr...@gmail.com> wrote:
> When deleting elements in a map is the ordering preserved. For
> example say I have elements A, B and C (order A, B, C) in a map and B
> is deleted, is the resulting ordering always A, C.
{ Edits: quoted clc++m banner removed. The banner is appended to every article
including this one (see below). Please don't quote extraneous material. -mod }
STL�s map data structure is binary search tree (balanced tree), it�s
always maintain it without bothering which data has been deleted. When
you traverse the map via iterator it will traverse the tree as left-
root-right. Therefore the out is always in the ascending order.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Saurabh
|
6/26/2008 2:45:06 AM
|
|
On Jun 25, 7:59 pm, Daniel <danwgr...@gmail.com> wrote:
> When deleting elements in a map is the ordering preserved. For
> example say I have elements A, B and C (order A, B, C) in a map and B
> is deleted, is the resulting ordering always A, C.
Yes, the order is guaranteed to be maintained.
However, if you have buggy code, you may get undefined behavior, in
which case the guarantee is revoked. Elements in an associative
container must have a strict weak ordering defined for their key,
either directly or parameterized with a template argument used for
comparisons. Most of the common types (strings, numbers, etc) already
have this ordering defined, so most of the time everything is just
fine--but if the user provides a key type whose comparisons do not
define a strict weak ordering, that's when there is a problem.
--
Chris
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Chris
|
6/26/2008 2:57:57 AM
|
|
|
4 Replies
295 Views
(page loaded in 0.269 seconds)
|