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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:46:32+00:00 2026-06-02T20:46:32+00:00

We can pass a function as < (less) operator to STL data structures such

  • 0

We can pass a function as <(less) operator to STL data structures such as set, multiset, map, priority_queue, …

Is there a problem if our function acts like <=(less_equal)?

  • 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-02T20:46:35+00:00Added an answer on June 2, 2026 at 8:46 pm

    From Effective STL -> Item 21. Always have comparison functions return false for equal
    values.

    Create a set where less_equal is the comparison type, then insert 10 into the set:

    set<int, less_equal<int> > s; // s is sorted by "<="
    s.insert(10); //insert the value 10
    

    Now try inserting 10 again:

    s.insert(10);
    

    For this call to insert, the set has to figure out whether 10 is already present. We know
    that it is. but the set is dumb as toast, so it has to check. To make it easier to
    understand what happens when the set does this, we’ll call the 10 that was initially
    inserted 10A and the 10 that we’re trying to insert 10B.The set runs through its internal data structures looking for the place to insert 10B. It ultimately has to check 10B to see if it’s the same as 10A. The definition of “the same”
    for associative containers is equivalence, so the set tests to see whether
    10B is equivalent to 10A. When performing this test, it naturally uses the set’s
    comparison function. In this example, that’s operator<=, because we specified
    less_equal as the set’s comparison function, and less_equal means operators. The set
    thus checks to see whether this expression is true:

    !(10A<= 10B)&&!(10B<= 10A) //test 10Aand 10B for equivalence
    

    Well, 10A and 10B are both 10, so it’s clearly true that 10A <= 10B. Equally clearly, 10B
    <= 10A. The above expression thus simplifies to

    !!(true)&&!(true)
    

    and that simplifies to

    false && false
    

    which is simply false. That is, the set concludes that 10A and 10B are not equivalent,
    hence not the same, and it thus goes about inserting 10B into the container alongside
    10A. Technically, this action yields undefined behavior, but the nearly universal
    outcome is that the set ends up with two copies of the value 10, and that means it’s not
    a set any longer. By using less_equal as our comparison type, we’ve corrupted the
    container! Furthermore, any comparison function where equal values return true will
    do the same thing. Equal values are, by definition, not equivalent!

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

Sidebar

Related Questions

Is there a function in less.js to pass a string of less code and
name_iexact keyword is a parameter we can pass to filter function in django.model. Can
I know you can pass arguments through the RunWorkerAsync function call when you first
I've created a wrapper function for jQuery's $.ajax() method so I can pass different
Can i pass the entire POST array into a function and handle it within
Can I pass some Javascript to a function and then execute that Javascript from
Can I pass a value out of a javascript function and back to the
Can I pass additional parameters to a predicate function? I need it in a
How can I pass a functions name to a function and then call it?
How can I pass the Parameter to a function. for example public void GridViewColumns(params

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.