Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki MarkupProviding an invalid ordering rule for an associative container or as a comparison criterion with the sorting algorithms can result in erratic behavior or infinite loops. (See \[Meyers01\] §21 for examples.)

Non-compliant Code Example 1

In this non-compliant example, the *IntSetLEtype defines a set with *less_equal* specified as the ordering rule.  Less Less than or equal is not a valid ordering rule because it violates the requirement to provide a "strict weak ordering" over the objects compared.   In particular, this ordering rule fails to return false for equal values.   As a result, the iterator pair returned by the *equal_range()* method are inversed inverted and the subsequent loop fails to terminate.

Code Block
typedef set<int, less_equal<int > > IntSetLE;

IntSetLE::const_iterator sleIter;
IntSetLE sle;

sle.insert(5);
sle.insert(10);
sle.insert(20);

pair<IntSetLE::const_iterator, IntSetLE::const_iterator> psle;

psle = sle.equal_range(10);

for (sleIter = psle.first; sleIter \!= psle.second; \++sleIter){
&nbsp;&nbsp;&nbsp;    cout << "Set contains: " << \*sleIter << endl;
}

...