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

  • SEARCH
  • Home
  • 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 8900179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:02:43+00:00 2026-06-15T01:02:43+00:00

I use data structures, and I sort these data structures a lot. These data

  • 0

I use data structures, and I sort these data structures a lot. These data structures are holding pointers to objects, not directly the objects themselves. Now I can write a simple comparison functor, or function, to tell the sort algorithm how to sort the pointers:

struct Object_ptr_comparer {
    bool operator()(const Object* first, const Object* second) {
        return *first < *second;
    }
};

And use for example std::sort:

Object_ptr_comparer comp;
std::sort(data_str.begin(), data_str.end(), comp);

The only problem with this solution that I have to write extra pointer comparator functor for any type of class. Yes, I could use inheritance and polymorphism to write only the comparator of some root class, but I don’t want to. Is there any other smart way to do this?

  • 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-15T01:02:44+00:00Added an answer on June 15, 2026 at 1:02 am

    That’s what templates are for!

    struct ptr_comparer {
        template<class Object>
        bool operator()(const Object* first, const Object* second) const {
            return std::less<Object>()(*first, *second);
        }
    };
    
    std::sort(data_str.begin(), data_str.end(), ptr_comparer());
    

    Since I’ve templated the operator rather than specializing the comparer directly, the compiler can deduce the types, so we don’t have to put the types directly.

    I use std::less rather than operator<, because it safely compares pointers to pointers (like char**), rather than relying on Undefined Behavior. std::less falls back on operator<, so it doesn’t add any complexity to calling code, and there should be no downside.

    I’m certain this one compiles

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

Sidebar

Related Questions

I wonder what is the best C# data structure I should use to sort
I have a question about the use of data structures such as ArrayLists in
How can I use data binding on a details form to add a new
All the books I've read on data structures so far seem to use C/C++,
I have a number of HashMap data structures containing hundreds of Comparable objects (say,
A data structure that I use commonly in multi-threaded applications is a ConcurrentHashMap where
Is there a .NET data structure I could use for bidirectional lookup? Here's the
I want to use pprint's output to show a complex data structure, but I
I have the need to use a Stack-like data structure for a program that
I use data binding to display values in text boxes in a C# Windows

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.