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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:42:00+00:00 2026-06-14T23:42:00+00:00

I am confused about strict weak ordering and how to use it when defining

  • 0

I am confused about strict weak ordering and how to use it when defining operator<. I have a couple of structs:

struct Plane
{
    std::string name;

    int xrudder;
    int yrudder;

    int wingwidgets;

    bool hasLegacyEngine;
};


struct Airport
{
    bool securityCheck;
    unsigned __int64 capacity;

    std::vector<Plane> planes;
};

and I want to create a std::set of Airports. I need to define operator< which uses strict weak ordering but I don’t know exactly what that means and/or how to do it.

struct cmpless
{
bool operator()(const Airport& left, const Airport& right)
    {
        //?
    }
}; 

std::set<Airport, cmpless> airportSet;

It doesn’t make sense that one airport is “less than” another. It only makes sense if the airports are equal based on their stats.

How can I be sure that my definition of operator< will follow strict weak ordering? How do I begin to think about defining operator< in a situation like this?

An example with an explanation would be great if possible!

  • 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-14T23:42:01+00:00Added an answer on June 14, 2026 at 11:42 pm

    If it “doesn’t make sense” for one Airport to come before another Airport then the use of std::set<Airport> doesn’t make sense, either. This container leverages the order amount elements to locate objects in O(log(n)) operations (where n is the size of the container). If you can identify object by identity only, the best complexity you can achieve is O(n). You can use a combination of std::find() or std::find_if() and one of the sequence containers, e.g., std::vector<Airport> or std::deque<Airport>.

    Since you don’t need to define an order in terms of operator<(), it may be reasonable to just bring the Airports into some order for the purpose of locating them in a std::set<Airport> which is done by using a different comparison function object than std::less<Airport>. The attribute you currently have in your Airport object don’t really look like suitable keys, though. In fact, they all look as if they would be mutable, i.e., you probably wouldn’t want a std::set<Airport> anyway because you can’t modify the elements in an std::set<T> (well, at least, you shouldn’t; yes, I realize that you can play tricks with mutable but this is bound to break the order of the elements).

    Based on this, I’d recommend to use a std::map<std:string, Airport>: the std::string is used to identify the airport, e.g., using the airport codes like "JFK" for the John F. Kennedy Airport in New York or "LHR" for London Heathrow. Conveniently, there is already a strict weak order defined on strings.

    That said, to define a strict weak order on a set of objects O, you need to a binary relation r(x, y) such that the following conditions hold for elements x, y, and z from O:

    • irreflexive: r(x, x) == false
    • asymmetric: r(x, y) == true implies r(y, x) == false
    • transitive: r(x, y) == true and r(y, z) == true implies r(x, z) == true
    • incomparability: r(x, y) == false and r(y, x) == false and r(y, z) == false and r(z, y) == false implies r(x, z) == false and r(z, x) == false

    The first three should be simple enough. The last one is a bit odd at first but actually not that hard either: The basic idea is that the relation doesn’t entirely order element but groups them into equivalent classes. If you think of the relation r to be “smaller than” it just says that if neither x is smaller than y nor y is smaller than x, then x and y are equivalent. The incomparable elements are just equivalent.

    The standard containers work with a strict weak order but, e.g., std::set<T> and std::map<K, V> keep just one version of equivalent keys. It is nice that this is sufficient but it is often simpler to just use a total order which is a strict weak order where for each pair of element x and y either r(x, y) == true or r(y, x) == true (but, due to the asymmetry not both).

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

Sidebar

Related Questions

I am a little confused about Accept-Encoding . I have Web Service which would
I am a little confused about where to use strong and where to use
I'm a little confused about how C++ handles includes. I have something like: typedef
I am confused about the interface of std::find . Why doesn't it take a
I'm learning the use of boost smart pointers but I'm a bit confused about
I am confused about the terminology of the below trees, I have been studying
Im a little bit confused about passing structs into functions. I understand pointers and
I'm still confused about the behaviour of std::vector::resize() . Consider the following code (see
I'm confused about when I need to use KeepAlive in my C++/CLI wrapper code
I was confused about how I would create two structs that reference each other.

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.