DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Sets - Sets(3C++)

Modifying elements in place

Casting away constants in order to modify a Set or Bag element in place is generally a dangerous practice:

       Setiter<int> a_iter(a);
       const int* ap;
   

while(ap = a_iter.next()){ *(int*)ap += 1; Suicide! }

The reason should now be clear: the location of an element within a Set or Bag is determined by its hash value. Changing the value of the element without changing its location violates an invariant of the Set or Bag data structure, causing the program to eventually fail in a way that will be quite difficult to trace. There is, however, one case when it is safe to cast away the constant, namely, when the data structure invariant does not involve the element value. This case occurs precisely when you don't provide a hash function to the Set or Bag structure.


Next topic: Internal representation of Sets
Previous topic: Space-time tradeoffs

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004