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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:20:25+00:00 2026-05-31T19:20:25+00:00

I get this problem while doing some experiment with set . I use a

  • 0

I get this problem while doing some experiment with set.

I use a structure with 2 integers as the key:

struct Key {
    int k1;
    int k2;
};

And use a class to build the ordering between keys:

struct keyComp {
    bool operator () (const struct Key& lhs, const struct Key& rhs) const {
        if (lhs.k1 < rhs.k1)
            return true;
        else if (lhs.k2 < rhs.k2)
            return true;
        else
            return false;
    }
};

But with this comparator, the set failed to find some existing keys. For example, in this program I store 9 keys in the set, from (0, 0) to (2, 2):

Key pos;
set <Key, keyComp> aset;

// insert elements from (0, 0) to (2, 2)
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        pos.k1 = i;
        pos.k2 = j;
        aset.insert (pos);
    }
}

// now try to find each of them
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        pos.k1 = i;
        pos.k2 = j;
        set <Key, keyComp> :: iterator it = aset.find (pos);
        if (it != aset.end ())
            cout << "v "; // element found
        else
            cout << "! "; // not found
    }
    cout << endl;
}

// print the set
cout << "element in set : ";
for (set <Key, keyComp> :: iterator it = aset.begin (); it != aset.end (); it++) {
    cout << "(" << it->k1 << ", " << it->k2 << ") ";
}
cout << endl;

I expect it would print 9 v‘s, which means all keys are found. But instead I got:

v v v 
! ! v 
! ! v 
element in set : (0, 0) (1, 0) (2, 0) (0, 1) (1, 1) (2, 1) (0, 2) (1, 2) (2, 2)

Some keys can be found, but some can’t even they are in the set.

Also, if I change the comparator to:

struct keyComp {
    bool operator () (const struct Key& lhs, const struct Key& rhs) const {
        // combine the two keys for comparison
        // say LARGE_NUMBER is a number bigger than all k2
        return lhs.k1 * LARGE_NUMBER + lhs.k2 < rhs.k1 * LARGE_NUMBER + rhs.k2;
    }
};

Then all keys are found.

Why is this happening? Is it because the original comparator failed to construct the order between keys?

  • 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-05-31T19:20:27+00:00Added an answer on May 31, 2026 at 7:20 pm

    Your comparator doesn’t provide a proper ordering, which means that the internals of set will do all manner of weird things (aka “undefined behaviour”) when trying to figure out where to insert or look-up things.

    You need something like this:

        if (lhs.k1 < rhs.k1)
            return true;
        if (lhs.k1 > rhs.k1)
            return false;
        return (lhs.k2 < rhs.k2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am running into this problem. I am trying to use $.get() to
I have been tackling this problem for some time. I get the image from
I am having a problem while doing some XSLT pre-processing in my java program.
I get this problem when referring to OpenFEC (openfec.org)-functions in my code: anders@ubuntu:~/workspace/fectest$ gcc
I periodically get this problem where all of a sudden mako is using old
Hope to get solution to this problem. I have been stuck on it since
I have a problem with this query trying to get a sum and a
This is my problem. Every time I load the page, I get an alert
This seems to be a recurring problem to me. I want to get started
This is a very old problem, but I cannot seem to get my head

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.