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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:55:24+00:00 2026-06-06T14:55:24+00:00

I am using a very simple structure, mapping, as defined below: struct mapping{ int

  • 0

I am using a very simple structure, mapping, as defined below:

struct mapping{
    int code;
    string label;

    bool operator<(const mapping& map) const {
        return code < map.code;
    }

    bool operator==(const mapping& map) const {
        return label.compare(map.label) == 0 ;
    }
};

I would like to create a set of mapping ordered by their code. For that, I have overloaded the < operator. It works fine. I can insert some mappings with different labels without any problem.

The problem come when I try to insert mappings with the same code but different label. Actually, in the second step of the process, I have no idea if a mapping with same label has been previously inserted. So, I need to call the find() function to determine if it is the case or not. If no mapping with same label has been inserted, it is ok, I just need to insert this new one (but its code will be temporarily the same than one other mapping). If one mapping with the same labels exists, I just need to update its code. I though that overloading the == operator as I did should be enough but it is not the case as illustrated by the following code.

mapping m = {1,"xxx"};
mapping m2 ={1, "yyy"};

this->fn[0].insert(m);

set<mapping>::iterator itTmp;
itTmp = this->fn[0].find(m2);
if (itTmp != this->fn[0].end() ) {
    cout << "m2 exists "<<endl;

    if ( !(*itTmp == m2) ){
        cout << "But it is different according to the definition of the == operator "<<endl;
    }
} 

Associated output is:

m2 exists
But it is different according to the definition of the == operator

How can I solve this issue and manage to deal with operators < and == with very different semantics? Ideally, I would like to avoid iterating on the whole set to look for mapping with the same label. Complexity of such a strategy is O(n) and n can be quite huge in my application. Similarly to the complexity of find() I would prefer a solution in O(log n).

Thanks,

Yoann

  • 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-06T14:55:24+00:00Added an answer on June 6, 2026 at 2:55 pm

    If a class can reasonably be sorted by more than one of its fields, it should not have relational operators (excepting operator== and operator!=) at all.

    Yes, std::map and std::set by default use std::less as the comparator (which uses operator< internally), but that’s just convenience so you don’t need to write a function object just to create a set<double>, where double has one canonical sort order (and therefore defines a (built-in) operator<).

    They also offer a comparator template argument, and you can and should use it to provide exactly the indexing that you need for that container (like a relational database). There are also containers (in boost) that maintain more than one index for a set of elements.

    As an example, consider a point2d class:

    struct point2d { int x, y; };
    

    One could reasonably want to index a container of point2ds by either x or y, so following the above, point2d shouldn’t define an operator< at all. To avoid having every user of the class inventing their own sorting function objects, they can be supplied together with point2d:

    struct less_point2d_by_x {
        typedef bool result_value;
        // the mixed overloads are for use in std::lower_bound and similar
        bool operator()( int lhs, int rhs ) const { return lhs < rhs; }
        bool operator()( int lhs, point2d rhs ) const { return lhs < rhs.x; }
        bool operator()( point2d lhs, int rhs ) const { return lhs.x < rhs; }
        bool operator()( point2d lhs, point2d rhs ) const { return lhs.x < rhs.x; }
    };
    // same for _y
    

    Usage:

    std::vector<point2d> v = ...;
    std::sort(v.begin(), v.end(), less_point2d_by_x()); // sort by x
    std::sort(v.begin(), v.end(), less_point2d_by_y()); // sort by y
    
    std::set<point2d, less_point2d_by_x> orderedByX;
    std::set<point2d, less_point2d_by_y> orderedByY;
    

    If you’re not afraid of templates, you can even templatise the relational operator, like this:

    template <template <typename T> class Op=std::less>
    struct point2d_by_x {
        typedef bool result_value;
        // the mixed overloads are for use in std::lower_bound and similar
        bool operator()( int lhs, int rhs )         const { return Op<int>()(lhs, rhs); }
        bool operator()( int lhs, point2d rhs )     const { return Op<int>()(lhs, rhs.x); }
        bool operator()( point2d lhs, int rhs )     const { return Op<int>()(lhs.x, rhs); }
        bool operator()( point2d lhs, point2d rhs ) const { return Op<int>()(lhs.x, rhs.x); }
    };
    // same for _y
    

    Usage:

    std::vector<point2d> v = ...;
    std::sort(v.begin(), v.end(), point2d_by_x<std::less>()); // sort ascending by x
    std::sort(v.begin(), v.end(), point2d_by_x<>()); // same
    std::sort(v.begin(), v.end(), point2d_by_x<std::greater>()); // sort descending by x
    // find the position where a `point2d` with `x = 14` would go in the ordering:
    std::lower_bound(v.begin(), v.end(), 14, point2d_by_x<std::greater>());
    
    std::set<point2d, point2d_by_x<std::less> > increasingXOrder;
    std::set<point2d, point2d_by_y<std::less> > increasingYOrder;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a very simple bit of code that mimics a class structure in
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have a very simple data structure that I have defined in Lisp: ;;Data
I've got a very simple data structure. I'm using SQLExpress with Linq2SQL and vb.net
I am using Delphi 2009. I have a very simple data structure, with 2
I'm using a very simple Stylesheet Switch by php. It was fine all along
It is very simple to do paging on access database (using absolute and pagesize)
Here's a very simple program using the function: #include <windows.h> #include <tchar.h> #include <atlstr.h>
Very simple question, but I want to start using a consistent naming convention for
I'm using PHP to submit a very simple data to db. On my page.php,

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.