Topic: iterators on sets


Author: jim.foley@NOSPAM_symbios.com
Date: 1998/07/07
Raw View
In sets, is it permissible to modify items through an iterator?  For
example, we have a compiler which permits the following:

  set<int> s1;
  for ( int i = 0; i < 10; i++ )
    s1.insert( i );
  cout << "s1 = " << s1 << endl;
  set<int>::iterator i1 = s1.find( 5 );
  int *p = i1.operator->();
  *p = 99;                         // MODIFYING A SET MEMBER!!
  cout << "s1 = " << s1 << endl;

This outputs (with operator<< suitably defined):

  s1 = ( 0 1 2 3 4 5 6 7 8 9 )
  s1 = ( 0 1 2 3 4 99 6 7 8 9 )

The tree has become unsorted.  Not good.

If the value type for a map is pair<const key_type,mapped_type>, so that
an iterator lets you modify the mapped_type value but not the key_type
value, then it seems reasonable that set iterators should also forbid you
from modifying the keys.

Borland C++ forbids it because it typedefs set iterators to be the same as
const_iterators, which seems a reasonable thing to do.

So does the standard explicitly permit or forbid the above behaviour?

--
Jim Foley                               Symbios, Fort Collins, CO
Jim.Foley@symbios.com                        (970) 223-5100 x9765

In theory, theory and practice are the same, in practice they're not.

--
Jim Foley                          jim.foley@NOSPAM_symbios.com
Assoc. Prof. of Omphalic Envy      Research interest:
Department of Anthropology         Primitive hominids
University of Ediacara             (Australopithecus creationistii)
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/07/08
Raw View
jim.foley@NOSPAM_symbios.com wrote in article <6ntp5u$2vo@herald.ks.symbios.com>...
> In sets, is it permissible to modify items through an iterator?  For
> example, we have a compiler which permits the following:
> .....
> The tree has become unsorted.  Not good.

More like ``disastrous.''

> If the value type for a map is pair<const key_type,mapped_type>, so that
> an iterator lets you modify the mapped_type value but not the key_type
> value, then it seems reasonable that set iterators should also forbid you
> from modifying the keys.

Quite right.

> Borland C++ forbids it because it typedefs set iterators to be the same as
> const_iterators, which seems a reasonable thing to do.
>
> So does the standard explicitly permit or forbid the above behaviour?

Dunno how explicit the wording is, but it certainly implicitly forbids such
behavior. (I say this knowing full well, from another thread, that the offending
compiler may well be VC++. If it permits this atrocity, then it's due to a
compiler bug.)

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



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]