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

The Archive Base Latest Questions

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

I found a bug in my code where I forgot to use a custom

  • 0

I found a bug in my code where I forgot to use a custom comparator when sorting a container of structs. This made me wonder what it was using as the less than operator, since I didn’t define any for the struct.

How do objects compare <, >, and == when those operators aren’t defined? Is it by memory address? Is it defined in the standard? I couldn’t find any of this information on Google.

EDIT:

Here’s the class that I was using:

using namespace std;

typedef unsigned id;

class LogEntry {
    id master_id;
    string timestamp;
    string category;
    string message;
    string str_rep;

public:
    LogEntry(id id, string t, string c, string m) :
            master_id(id), timestamp(t), category(c), message(m) {
    }

    string get_timestamp() const {
        return timestamp;
    }

    string get_category() const {
        return category;
    }

    string get_message() const {
        return message;
    }

    string to_string() {
        ostringstream ss;
        ss << master_id << "|" << timestamp << "|" << category << "|"
                << message;
        return ss.str();
    }

    id get_id() const {
        return master_id;
    }
};

EDIT2:

I realized I made a dumb mistake. I was storing a vector of pointers to the objects. Thus, it’s very likely that the pointers are compared by the address. If I hadn’t been storing pointers, I don’t think it would have compiled.

EDIT3:
KerrekSB posted a related interesting link in the comments of his answer that is related: How can pointers be totally ordered?

  • 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-14T13:28:01+00:00Added an answer on June 14, 2026 at 1:28 pm

    The default comparator is the standard template std::less<T>, which just uses x < y for two objects x and y of type T. There are many ways this could work:

    • T is an arithmetic, fundamental type and the built-in operator is used.

    • T is a class type and has a member operator<.

    • There is a free function operator<(T const &, T const &).

    • Your user-defined type has an implicit conversion function to a built-in type which provides a unique path for calling the built-in <.

    Additionally, it is possible to specialize std::less for your user-defined type T.

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

Sidebar

Related Questions

I found a bug in my code: This code replaces some regular text with
Just found a bug in this code : for(String link : tempList1){ if(!tempList2.contains(link));{ listToPopulate.add(link);
I found this bug error while building an application for iPhone using c#. This
Hi recently I found this bug in my code. this link http://www.zendez.com/beta/files/retreatphotos/larges/idretreat70/Mayacamas Ranch, Calistoga,
Before anyone answers this , I found the bug in my code, and posted
We've found a bug in old code where connections aren't being closed. It's an
Today I found a very mysterious bug in the code. I have used the
While searching a bug in my code today I found a strange thing. When
I found a bug in my code where I compared the pointer with '\0'.
I found a bug in my JavaScript code that I have isolated to 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.