Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong>: using c++14, the best solution is very easy to write thanks to lambdas that can now have parameters of type <code>auto</code>. <strong>This is my current favorite solution</strong></p> <pre><code>std::sort(v.begin(), v.end(), [](auto &amp;left, auto &amp;right) { return left.second &lt; right.second; }); </code></pre> <hr> <p>Just use a custom comparator (it's an optional 3rd argument to <code>std::sort</code>)</p> <pre><code>struct sort_pred { bool operator()(const std::pair&lt;int,int&gt; &amp;left, const std::pair&lt;int,int&gt; &amp;right) { return left.second &lt; right.second; } }; std::sort(v.begin(), v.end(), sort_pred()); </code></pre> <p>If you're using a C++11 compiler, you can write the same using lambdas:</p> <pre><code>std::sort(v.begin(), v.end(), [](const std::pair&lt;int,int&gt; &amp;left, const std::pair&lt;int,int&gt; &amp;right) { return left.second &lt; right.second; }); </code></pre> <p><strong>EDIT</strong>: in response to your edits to your question, here's some thoughts ... if you <strong>really</strong> wanna be creative and be able to reuse this concept a lot, just make a template:</p> <pre><code>template &lt;class T1, class T2, class Pred = std::less&lt;T2&gt; &gt; struct sort_pair_second { bool operator()(const std::pair&lt;T1,T2&gt;&amp;left, const std::pair&lt;T1,T2&gt;&amp;right) { Pred p; return p(left.second, right.second); } }; </code></pre> <p>then you can do this too:</p> <pre><code>std::sort(v.begin(), v.end(), sort_pair_second&lt;int, int&gt;()); </code></pre> <p>or even</p> <pre><code>std::sort(v.begin(), v.end(), sort_pair_second&lt;int, int, std::greater&lt;int&gt; &gt;()); </code></pre> <p>Though to be honest, this is all a bit overkill, just write the 3 line function and be done with it :-P</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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