Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Extending on jrok's answer, if you encounter similar problems more often, a reusable templated comparator which uses <em>any member</em> of a class comes in very handy.</p> <pre><code>template&lt;class T, class U&gt; class CompareByMember { U (T::*mem); // ugly syntax for member pointer of type U in class T public: CompareByMember(U (T::*mem)) : mem(mem) {} bool operator()(const T &amp;a, const T &amp;b) { return (a.*mem) &lt; (b.*mem); // ugly syntax for member pointer access } }; </code></pre> <p>As you can see, the syntax for pointers to class members is pretty strange, but once wrapped in this functor you don't have to care about it. One remaining "issue" is that you'd have to write the template parameters <code>&lt;T, U&gt;</code> each time you want to use this functor. But using type deduction this problem is solved, introducing a little helper function (note that its name is lower case):</p> <pre><code>template&lt;class T, class U&gt; CompareByMember&lt;T,U&gt; compareByMember(U (T::*mem)) { return CompareByMember&lt;T,U&gt;(mem); } </code></pre> <p>This results in client code like this:</p> <pre><code>std::vector&lt;EdgeExtended&gt; v = ...; std::sort(v.begin(), v.end(), compareByMember(&amp;EdgeExtended::neighborNodeId)); </code></pre> <p><a href="http://ideone.com/iQher7" rel="nofollow"><strong>Simple demonstration</strong></a></p> <p>For member <em>functions</em>, a similar templated functor can be written, using the only slightly different member function pointer syntax. You can also overload the call operator in the functor to accept raw pointers of <code>T</code> as well as any smart pointer wrapping around <code>T*</code> (using templates again).</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.
    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