Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could also do this very easy with lambdas (C++0x/11):</p> <pre><code>vector&lt;Character&gt; v = ...; sort(v.begin(), v.end(), [](const Character &amp;lhs,const Character &amp; hs){ return lhs.getType() == rhs.getType() ? lhs.getScore() &lt; rhs.getScore() : lhs.getType() &lt; lhs.getType(); }); </code></pre> <p>Or for pointers:</p> <pre><code>vector&lt;Character*&gt; v = ...; sort(v.begin(), v.end(), [](const Character *lhs,const Character * hs){ return lhs-&gt;getType() == rhs.getType() ? lhs-&gt;getScore() &lt; rhs-&gt;getScore() : lhs-&gt;getType() &lt; lhs-&gt;getType(); }); </code></pre> <hr> <p>I couldn't find a "criteria combiner" so I wrote one on my own. This should work:</p> <pre><code>template&lt;class T, class First, class Second&gt; class combine { First first; Second second; public: bool operator()(const T &amp; lhs, const T &amp; rhs) const { if(first(lhs, rhs)) return true; if(first(rhs, lhs)) return false; return second(lhs, rhs); } }; </code></pre> <p>With two basic comparison functors</p> <pre><code>struct less_type { static bool operator()(const Character &amp; lhs, const Character &amp; rhs) { return lhs.getType() &lt; rhs.getType(); } }; struct less_score { static bool operator()(const Character &amp; lhs, const Character &amp; rhs) { return lhs.getScore() &lt; rhs.getScore(); } }; </code></pre> <p>this can be used like the following:</p> <pre><code>vector&lt;Character*&gt; v = ...; sort(v.begin(),v.end(), combine&lt;Character,less_type,less_score&gt;()); </code></pre> <p>This will also work without support for C++0x lambdas.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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