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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:47:08+00:00 2026-06-10T03:47:08+00:00

I am using sets. I use a custom struct as the key. I am

  • 0

I am using sets. I use a custom struct as the key. I am inserting a value and trying to find the inserted value. But it never seems to find the element.

I have overridden both the == operator and the < operator.

Here is the code of the structure:

struct distance_t
{
 public:
int id;
double distance;

bool operator<(const distance_t& rhs) const
{
    if(distance < rhs.distance)
        return true;
    else 
        return false;
}

bool operator==( const distance_t& rhs) 
{
    if(id == rhs.id)
        return true;
    else
        return false;
}
};

And this is the code of main

int main()
{
    set<distance_t> currentSet;

    distance_t insertDistance;
    insertDistance.id =1;
    insertDistance.distance = 0.5;

    currentSet.insert(insertDistance);

    distance_t findDistance;
    findDistance.id = 1;

    assert(currentSet.find(findDistance) != currentSet.end());
}

It always fails in the assert statement. What am I doing wrong?

Edit -Ok now I understand that it does not use the == operator at all. Here is what I want. I need the data structure to be ordered by distance. But I should be able to remove it using the id. Is there any clean way or already existing datastructure 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-10T03:47:09+00:00Added an answer on June 10, 2026 at 3:47 am

    It fails because your less-than comparison uses distance_t::distance, which you are not setting in findDistance:

    distance_t findDistance;
    findDistance.id = 1;
    

    std::set does not use operator== for anything. It only uses operator<. So you would have to change it’s logic to use distance_t::id.

    If you want to search by id without changing the set’s ordering, you can use std::find:

    set<distance_t>::iterator it = std::find(currentSet.begin(), 
                                             currentSet.end(), 
                                             findDistance);
    

    This will use your operator==. Bear in mind that this has linear time complexity.

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

Sidebar

Related Questions

Unowned one-to-many and many-to-many relationships are defined using Sets or Lists of Keys. Let's
we are using data sets as our data access layer. currently there are some
I'm using nested sets (aka modified preorder tree traversal) to store a list of
I am using a function which sets a cookie. This function allows the cookie
I am using jquery to create two sets of tabs on a page. The
I am using GridView for displaying groups of different sets of items in a
I wrote a simple plugin which sets some css code using wp_options. It all
In my MVC3 Application I am using a controller actionresult to return two sets
I've got a script that sets some session values before redirecting to / using
I am working on a filter functionality using ajax/jquery and php/mysql.I have two sets

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.