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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:27:58+00:00 2026-05-16T22:27:58+00:00

I have a few integer keys that is needed to retrieve a value. What

  • 0

I have a few integer keys that is needed to retrieve a value. What is the most efficient way to store and retrieve the value based on the keys?

Currently I am converting and combining the three keys into a single string and store the key-value pair in a map. However, I think there should be a more efficient way of doing that based on the integer value alone for example generating a single key based on the three keys. Any ideas on that?

Thanks.

  • 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-16T22:27:59+00:00Added an answer on May 16, 2026 at 10:27 pm

    You can almost certainly do better than converting to a string (which is generally quite a slow operation). If your three items have small enough ranges, you can do some shifting and adding to put them together into a single integer with a larger range.

    Lacking that, you can just create a struct that holds three ints, and defines operator< to compare them in a reasonable fashion:

    class key { 
        int a, b, c;
    public:
        bool operator<(key const &other) { 
            if (a < other.a)
                return true;
            if (a > other.a)
                return false;
            if (b < other.b)
                return true;
            if (b > other.b)
                return false;
            if (c < other.c)
                return true;
            return false;
        }
    };
    

    Edit: for those who care about it, using (std | tr1 | boost)::tuple wouldn’t change the character of answer much, but would eliminate most of the code. A tuple is pretty similar to std::pair, except that it supports an arbitrary number of items instead of just two. For the example above, you could use something like:

    #include <tuple>
    
    namespace stdx = std::tr1;    // or stdx=boost::tr1, etc.
    
    typedef stdx::tuple<int, int, int> key_t;
    
    std::map<key_t, whatever> my_map;
    

    tuple defines comparison operators automatically assuming the types are comparable (i.e., for comparing two tuples with the same number of elements, and the types of corresponding elements are comparable). In this case it does a lexicographic comparison (i.e., the result of the comparison is the result from the first pair of corresponding elements that are not equal — of no such element exists, the two compare equal).

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

Sidebar

Related Questions

I have a few Visual Studio Solutions/Projects that are being worked on in my
I have a few C# .dll projects which are common to many applications. Currently,
We have a few projects that involve building an application that is composed of
I have a few lines of PowerShell code that I would like to use
I have an application that sends data based on user interaction (not user input).
In C#, I have an array of integers, that represent the primary keys of
I have a few inequalities regarding {x,y} , that satisfies the following equations: x>=0
I have few asynchronous tasks running and I need to wait until at least
I have few different applications among which I'd like to share a C# enum.
I'm a newbie to pgsql. I have few questionss on it: 1) I know

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.