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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:38:14+00:00 2026-06-02T09:38:14+00:00

I have a problem with the operator<() method which is required for a std::map.

  • 0

I have a problem with the operator<() method which is required for a std::map. I’m using a struct as composite key that looks as follows:

struct MyKey {
  std::string string1;
  std::string string2;
  std::string string3;
  unsigned int uint1;

  friend bool operator<(const MyKey& mk1, const MyKey& mk2)
  {
    return mk1.string1 < mk2.string1 && mk1.string2 < mk2.string2 &&
           mk1.string3 < mk2.string3 && mk1.uint1 < mk2.uint1;
  }
}

As introduced I want to use a composite key with 4 values, but I don’t know how to achieve this for the operator< method. I observed that only 1 value is stored at a time!

Can anybody tell me how the right condition looks like?

Thanks in advance!

  • 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-02T09:38:17+00:00Added an answer on June 2, 2026 at 9:38 am

    The Standard library’s associative containers such as std::map, std::set, std::multiset, std::multimap, std::bitset require that the ordering of elements must follow Strict Weak Ordering, which means your implementation of operator< must follow strict weak ordering. So one implementation could be this:

    friend bool operator<(const MyKey& mk1, const MyKey& mk2)
    {
      if (mk1.string1 != mk2.string1 )
           return mk1.string1 < mk2.string1;
    
      else if ( mk1.string2 != mk2.string2)
           return mk1.string2 < mk2.string2;
    
      else if (mk1.string3 != mk2.string3)
           return  mk1.string3 < mk2.string3;
    
      else
           return mk1.uint1 < mk2.uint1;
    }
    

    Or you can implement it as:

    friend bool operator<(const MyKey& mk1, const MyKey& mk2)
    {
      auto const & t1 = std::tie(mk1.string1, mk1.string2, mk1.string3, mk1.uint1);
      auto const & t2 = std::tie(mk2.string1, mk2.string2, mk2.string3, mk2.uint1);
      return t1 < t2;
    }
    

    In this solution, std::tie function creates two tuples t1 and t1 of the references of the arguments passed to it, and then compare t1 and t2 using overloaded operator< for std::tuple instead. The operator< for tuple compares the elements lexicographically — strict-weak ordering is achieved..

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

Sidebar

Related Questions

I have problem with Android using the class HttpUrlConnection my method is this: public
I have a problem when I call operator<< on a pointer. I have searched
I have a problem with a bash script. I have to use the operator
I'm facing a problem in Xcode with the operator keyword. I have this in
I have a problem with operators. Generally I know basics of using operators. But
I have problem in some JavaScript that I am writing where the Switch statement
I have a problem in my code which i can't understand. I want to
I have an operator class that has some classes and methods in it that
I have a weird problem with my code as the compiler generates code which
I've been playing around with Expression Trees. I have the following simple method that

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.