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 8838471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:00:16+00:00 2026-06-14T10:00:16+00:00

I would like to modify the following code so that I can sort using

  • 0

I would like to modify the following code so that I can sort using objects.
The current code is fine when sorting individual methods that return a single value.
How can I implement using methods that returns a object;

template<typename T, typename M, template<typename> class C = std::less>
struct method_comparer : std::binary_function<T, T, bool>
{
explicit method_comparer(M (T::*p)() const) : p_(p) { }

bool operator ()(T const& lhs, T const& rhs) const
{
return C<M>()((lhs.*p_)(), (rhs.*p_)());
}

private:
M (T::*p_)() const;
};



template<typename T, typename M>
method_comparer<T, M> make_method_comparer(M (T::*p)() const)
{
return method_comparer<T, M>(p);
}

template<template<typename> class C, typename T, typename M>
method_comparer<T, M, C> make_method_comparer2(M (T::*p)() const)
{
 return method_comparer<T, M, C>(p);
 }

Main.cpp

// works well
std::sort(vec_p2d.begin(),vec_p2d.end(),make_method_comparer(&Point2D::getX)); 

// Would like to implement this

 std::sort(vec_l2d.begin(),vec_l2d.end(),make_method_comparer(&Line2D::getPt1)); 

getPt1() methods return a Point2D object which contains the values for int x and int y;

  • 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-14T10:00:18+00:00Added an answer on June 14, 2026 at 10:00 am

    AFAICS, you can leave your code as is. The only thing you must define is a comparison operator for Point2D or whatever object you’re returning:

    class Point2D {
    public:
        friend bool operator<(const Point2D &p1, const Point2D &p2) { ...; }
        ...
    };
    

    You can also remove your method_comparer class and just give appropriate comparer functions to sort:

    bool compare_line_points(const Line2D &l1, const Line2D &l2) {
        return l1.getPt1() < l2.getPt1();
    }
    

    and

    std::sort(vec_l2d.begin(), vec_l2d.end(), compare_line_points);
    

    Depending on your requirements, these are just a bunch of one- or two-liners. No need for templates.

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

Sidebar

Related Questions

I have code, similar to the following, that I would like to modify: Sub
I have various methods that I would like to unit test using Visual Studio's
I have some code (that I can't easily modify), of the following form: def
We have a customer that would like to modify application user messages that we
I'm using HighLine to write my console application and I would like to modify
I would like to select a node and modify its attributes and child-nodes using
I have several PDF templates that I would like to load and modify and
I would like modify HTML like I am <b>Sadi, novice</b> programmer. to I am
I would like to modify an object private variable class Example(): __myTest1 = 1
I'm absolutely new to JavaScript and would like to modify a textarea of a

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.