Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ How to Sort Vector making use of Template
    primarykey
    data
    text
    <p>C++ How to Sort Vector making use of Template</p> <p>Hi Guys, thanks for looking at my question.</p> <p>I got a Templates.h file that goes this way..</p> <pre><code>/* Template Less Than */ template&lt;typename T&gt; bool lessThan(T a,T b) { return a&lt;b; } /* Template greater Than */ template&lt;typename T&gt; bool greaterThan(T a,T b) { return a&gt;b; } /* Template Equals */ template&lt;typename T&gt; bool equals(T a,T b) { return a==b; } </code></pre> <p>Then i got this class</p> <pre><code>Map2D </code></pre> <p>About Map2D..</p> <pre><code>class Map2D { protected: int x; int y; public: Map2D(); Map2D(int,int); int getX(); int getY(); }; </code></pre> <p>At my main.cpp i got a vector class of Map2D</p> <pre><code>vector&lt;Map2D&gt; map2d; </code></pre> <p>So now i need to sort it by X Ascending.. how do i make use of the template file to do a sort on the vector of it X Ascending.. Consider i will need overload another for DESCENDING later..</p> <p>Normally i will use </p> <pre><code>sort(map2d.begin(),map2d.end(),sortByX); </code></pre> <p>and sortByX will be a struct with overload by it () operator.</p> <p>But the question now is since i got a template that is lesser than and greater than.. how can i make use of it to sort X by ascending and then another X by descending with the template generic function of Templates.H .</p> <p><strong>Updates:</strong></p> <p>I think i need to overload the class Map2D operator > , &lt; and ==</p> <p>but my question is how do i overload it with the help of MyTemplates.h function such as lesserThan , greaterThan, equals</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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