Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::list Strict Weak Ordering
    text
    copied!<p>I am having a lot of trouble using the std::list::sort function, it works a majority of the time, however every once in a while it throws an assertion 'invalid operator&lt;'. Looking into this issue I have realized it is because my sort function is not following strict weak ordering, however when I look at my code I do not understand why it is not following strict weak ordering as it seems correct, what is it I am missing here?</p> <p>The purpose of this function is to sort a list of elements into a formula string based on the hill system, ie. Carbon First, Hydrogen Second, all others alphabetically. The FormulaStruct simply represents a single element and amount in the full formula.</p> <pre><code>struct FormulaStruct { FormulaStruct(const std::string &amp; strSymbol, int nNum, bool bHasCarbon) : m_strSymbol(strSymbol), m_nNum(nNum), m_bHasCarbon(bHasCarbon) { } bool operator &lt; (const FormulaStruct &amp; rhs) { //If the symbols are equal if(m_strSymbol == rhs.m_strSymbol) return true; if(m_bHasCarbon) { if(m_strSymbol == "C") return true; else if(rhs.m_strSymbol == "H") return false; } return m_strSymbol &lt; rhs.m_strSymbol; } bool operator == (const FormulaStruct &amp; rhs) { return m_strSymbol == rhs.m_strSymbol; } std::string m_strSymbol; int m_nNum; bool m_bHasCarbon; }; list&lt;FormulaStruct&gt; FormulaList; //A list of FormulaStructs, assumed to be filled FormulaList.sort(); </code></pre> <p><strong>EDIT</strong> bHasCarbon is the condition when there is carbon in the formula, as the hill system requires that if there is carbon in the formula than hydrogen will be next, otherwise everything is alphabetical including hydrogen, this is dictated in another section of my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload