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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:33:39+00:00 2026-06-12T09:33:39+00:00

I would like to have a user-defined key in a C++ std::map . The

  • 0

I would like to have a user-defined key in a C++ std::map. The key is a binary representation of an integer set with maximum value 2^V so I can’t represent all 2^V possible values. I do so by means of an efficient binary set representation, i.e., an array of uint64_t.

Now the problem is that to put this user-defined bitset as key in a std::map, I need to define a valid comparison between bitset values but if I have a maximum size of, say, V=1000, then I cannot get a number I can compare, let alone aggregating them all i.e., 2^1000 is not representable.

Therefore my question is, suppose I have two different sets (by setting the right bits in my bitset representation) and I cannot represent the final number because it will overflow:

id_1 = 2^0 + 2^1 + ... + 2^V

id_2 = 2^0 + 2^1 + ... + 2^V

Is there a suitable transformation that would lead to a value I can compare? I need to be able to say id_1 < id_2 so I would like to transform a sum of exponentials to a value that is representable BUT maintaining the invariant of the “less than”. I was thinking along the lines of e.g. applying a log transformation in a clever way to preserve “less than”.

Here is an example:

set_1 = {2,3,4}; set_2 = {8}

id(set_1) = 2^2 + 2^3 + 2^4 = 28; id(set_2) = 2^8 = 256

id(set_1) < id(set_2)

Perfect! How about a general set that can have {1,...,V}, and thus 2^V possible subsets?

  • 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-12T09:33:41+00:00Added an answer on June 12, 2026 at 9:33 am

    I do so by means of an efficient binary set representation, i.e., an array of uint64_t.

    Supposing that this array is accessed via a data member ra of the key type Key, and both arrays are of length N, then you want a comparator something like this:

    bool operator<(const Key &lhs, const Key &rhs) {
        return std::lexicographical_compare(lhs.ra, &lhs.ra[N], rhs.ra, &rhs.ra[N]);
    }
    

    This implicitly considers the array to be big-endian, i.e. the first uint64_t is the most significant. If you don’t like that, that’s fair enough, since you might already have in mind some relative significance for whatever order you’ve stored your V bits into your array. There’s no great mystery to lexicographical_compare, so just look at an example implementation and modify as required.

    This is called “lexicographical order”. Other than the facts that I’ve used uint64_t instead of char and both arrays are the same length, it is how strings are compared[*] — in fact the use of uint64_t isn’t important, you could just use std::memcmp in your comparator instead of comparing 64-bit chunks. operator< for strings doesn’t work by converting the whole string to an integer, and neither should your comparator.

    [*] until you bring locale-specific collation rules into play.

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

Sidebar

Related Questions

I would like to have a text field that a user can enter a
To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide
I have made some user-defined exceptions and I would like to add an error
We would like to have user defined formulas in our c++ program. e.g. The
Still an iphone dev starter but trying. I would like to have the user
I would like to have the page on user/sidebar on the right in the
I would like to have a checkbox preference that takes the user to a
I currently have two Backbone models: user and project. I would like to have
I would like to make my user control have invisible areas like the areas
I have a MySQL user added to a database that I would like to

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.