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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:49:47+00:00 2026-05-20T00:49:47+00:00

I am trying to figure out the best data structure to use for this

  • 0

I am trying to figure out the best data structure to use for this problem. I am implementing a key value store with keys that are strings. The values get added frequently and will generally only get looked up 1 or 2 times. Initially I used an std::map, but I found the performance to be unoptimal, as the overhead of adding keys and rebalancing the red-black tree, overshadowed the decrease in time to search for a value. Currently I am using a modified single linked list. It uses a struct that contains a c string (const char *), the length in bytes, and the value stored. When I want to find a value using a key I iterate through the list and compare the size of the keys, if they match I use memcmp to check if the strings are identical. If they are identical, I return the value. I way able to achieve about 10x greater performance using this method over the std::map. I need to make it about 2x more efficient, however. Can anyone recommend a better type of data structure, for this problem?

  • 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-20T00:49:48+00:00Added an answer on May 20, 2026 at 12:49 am

    It is hard to come with a fast solution without any knowledge on the actual problem. In particular, how big is your dataset, where is the real data stored (is it stored in the container or somewhere else?). What other operations do you need to perform on the container? Do you need to delete elements from the container?

    As a comment to one of the other questions you state that the keys need to be copied in std::unordered_map… if the keys are already stored somewhere else, I would advice you to use a map, but avoid copying the strings. Use pointers as the keys, and a custom comparator to dereference and operate in the result:

    // Assuming that the data is stored in std::string somewhere else
    struct custom_compare {
       bool operator()( std::string* lhs, std::string* rhs ) const {
          return lhs!=rhs && (lhs->size() < rhs->size() || lhs->compare( *rhs ) < 0);
       }
    };
    std::map< std::string*, data, custom_compare > mymap;
    

    By storing pointers instead of the actual strings this would take rid of copying. The custom comparator is basically as fast as the one you have implemented in the list and the tree will balance the contents, allowing for O(log n) lookups. Depending on the size of the set (if there are many elements) then this will be an improvement over linear search, while if the size is small then linear search will be better.

    Also, depending on the diversity of the data, you might want to follow the linear search but divide the search space depending on some criteria that is fast to calculate and at the same time divides the set as evenly as possible. For example, you could use linear search, but instead of keeping a single list, keep different lists based on key length.

    If the criterion is actually based on the contents of the string (letters, rather than size) then you are approximating the definition of a trie. If you get a library that already implements one, or you are willing to spend the time required to do so, a trie will probably be one of the fastest containers for this type of lookup, as it transforms the “size” variable from number of elements to length of the strings.

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

Sidebar

Related Questions

I am trying to figure out the best way to store my application key
I am trying to figure out the best way to parse this comma delimited
I'm trying to figure out how to best lay this out. I'll explain what
I am trying to figure out the best way to deal with this. First,
Folks, I'm trying to figure out a pattern or best practice to translate data
I'm trying to figure out the best way to determine whether I'm in the
I am trying to figure out the best way to model a spreadsheet (from
I'm trying to figure out the best way to insert a record into a
I'm trying to figure out the best lower-budget home office development hardware setup. My
I am trying to figure out the best way in an asmx web service

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.