int main() { TSetOf<TStandardText>* collection = new TSetOf<TStandardText>(NIL, NIL); collection->Add(new TStandardText("one")); collection->Add(new TStandardText("two")); collection->Add(new TStandardText("three")); collection->Add(new TStandardText("one")); collection->Add(new TStandardText("two")); collection->Add(new TStandardText("three")); printCollection(collection); delete collection; return 0; }
0> three 1> one 2> three 3> two 4> two 5> one
The order of elements is unordered, as it should be. However, the set appears to contain duplicate elements. The problem is that the default mechanism used by collections compares pointer addresses of collection elements to determine if two elements are equal. In some cases this definition of equality makes sense. However if the intent of a program like the one above is to weed out duplicates, use another approach. Because each of the calls to the new operator above returns a different pointer, all six pointers in the collection have a different address. Collection elements are not equal for the same reason that the expression
(new TStandardText("one")) == (new TStandardText("one"))
int main() { TSetOf<TStandardText>* collection = new TSetOf<TStandardText>(NIL, NIL);
0> two 1> one 2> three