Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot (jp)

Providing 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 §21 for examples.)

Non-Compliant Code Example

...

Code Block
bgColor#FFcccc
typedef set&lt;intset<int, less_equal&lt;int &gt; &gt;equal<int > > IntSetLE;

IntSetLE::const_iterator sleIter;
IntSetLE sle;

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

pair&lt;IntSetLEpair<IntSetLE::const_iterator, IntSetLE::const_iterator&gt;iterator> psle;

psle = sle.equal_range(10);

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

Compliant Solution

...

Code Block
bgColor#ccccff
typedef set&lt;intset<int, less&lt;int&gt; &gt;less<int> > IntSetLess;

IntSetLess::const_iterator islIter;
IntSetLess isl;

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

pair&lt;IntSetLesspair<IntSetLess::const_iterator, IntSetLess::const_iterator&gt;iterator> pisl;

pisl = isl.equal_range(10);

for (islIter = pisl.first; islIter \!= pisl.second; \++islIter) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &lt;&lt; &quot;<< "Set contains: &quot; &lt;&lt;" << \*islIter &lt;&lt;<< endl;
}

Risk Assessment

Using an invalid ordering rule can lead to erratic behavior or infinite loops.

...