Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:27+00:00 2026-06-14T11:28:27+00:00

I have a header, which consists of different template functions #include <cmath> template<class T>

  • 0

I have a header, which consists of different template functions

#include <cmath>

template<class T>
bool lessThan(T x, T y) {

    return (x < y);

}

template<class T>
bool greaterThan(T x, T y) {

    return (x > y);

}

A class

class Point2D {
public:
    Point2D(int x, int y);
protected:
    int x;
    int y;
    double distFrOrigin;

In my driver class, I have an STL List of Point2D: list<Point2D> p2dL. How do I sort p2dL using the template functions lessThan and greaterThan in my header? i.e. Sort the list based on x or y value.

EDIT: And so, based on Anton’s comment, I came up with this:

bool Point2D::operator<(Point2D p2d) {

    if (this->x < p2d.x || this->y < p2d.y
            || this->distFrOrigin < p2d.distFrOrigin) {

        return true;

    }

    else {

        return false;

    }

}

Did I do it correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:28:28+00:00Added an answer on June 14, 2026 at 11:28 am

    First, all three major templates can be exposed using just an operator <() so long as you enforce strict ordering:

    template<class T>
    bool lessThan(const T& x, const T& y) 
    {
        return (x < y);
    }
    
    template<class T>
    bool greaterThan(const T& x, const T& y) 
    {
       return (y < x);
    }
    
    template<class T>
    bool equals(const T& x, const T& y) 
    {
       return !(x < y) || (y < x));
    }
    

    Next, your class must implement operator <() to compare a *this against a parameter. A sample appears below:

    class Point2D {
    public:
        Point2D(int x, int y);
    
        // sample that orders based on X primary, and Y if X's are equal.
        bool operator <(const Point2D& other) const
        {
            return (x < other.x || (x == other.x && y < other.y));
        }
    
    protected:
        int x;
        int y;
        double distFrOrigin;
    };
    

    Lastly. sort your list like so:

    // sort ascending
    std::sort(p2dl.begin(), p2dl.end(), lessThan<Point2D>);
    
    // sort descending
    std::sort(p2dl.begin(), p2dl.end(), greaterThan<Point2D>);
    

    Or as Juan pointed out, use the list-sort directly:

    p2dl.sort(lessThan<Point2D>);
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have an Application which consists of a BasePage which as a header
I have a header which I constructed like this: <header class=top> <a href=> <span
I have a page in which a header consists of three divs - one
In my app I have a header bar which consists of a single textview
Here is one of my header file which consists of a union template with
I have page which consists of couple fragments and in the header fragment I
I have a table which consists of a header row, a footer row and
I have a header area which is divided up into block areas for images,
I have a header.html which start with session_start();. Then the following code, $_SESSION['cart'][$sw_id] is
Hope someone has an easy answer on this. I have a header image which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.