Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While solving the compiler error simply requires the <code>operator()</code> to be public as according to most other answers to this question, you should note that your comparator isn't a valid <em>strict</em> comparator. A strict comparator has to return <code>false</code> for elements which are treated as being equal. You should also use const references instead of values for the parameter types of your comparator.</p> <p>I want to present you multiple options to solve your problem (not the compiler-error but the original problem, sorting a string in reversed lexical order). All of them return <code>false</code> for two equal elements.</p> <p>The <strong>first option</strong> is your solution but I fixed the strictness:</p> <pre><code>template &lt;typename T&gt; class ReverseComparator{ public: bool operator()(const T&amp; l, const T&amp; r){return (r &lt; l);} }; std::sort(str.begin(), str.end(), ReverseComparator()); </code></pre> <p>The <strong>second option</strong> uses C++11's <em>lambda function</em>, but is essentially the same:</p> <pre><code>std::sort(str.begin(), str.end(), [](const T&amp; l, const T&amp; r){return (r &lt; l);}); </code></pre> <p>As a <strong>third option</strong>, as first proposed by Mooning Duck, you could simply use the std functor for <code>operator&gt;</code> on your type <code>char</code>:</p> <pre><code>std::sort(str.begin(), str.end(), std::greater&lt;char&gt;()); </code></pre> <p>The <strong>fourth option</strong> (the simplest one) would be to use the default comparator on the <em>reversed</em> string, as first proposed by Peter Wood (note the <code>r</code>!):</p> <pre><code>std::sort(str.rbegin(), str.rend()); </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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