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

  • Home
  • SEARCH
  • 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 8394207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:59:47+00:00 2026-06-09T19:59:47+00:00

I need to be able to store and lookup generic strings. I don’t know

  • 0

I need to be able to store and lookup generic strings. I don’t know much about the content of the strings, a little more then 2/3 are human language words with the rest being something closer to a UUID or number/letter combo. I know that any particular grouping will be constant (ie if it has some human words it will be all human words, if it has some UUID all the contents will be UUIDs etc).

I need to decide if I should place this data in a map or a hashmap to get the best average lookup rate. I’m inclined to say map with the O(log n) runtime because I don’t believe I can make a proper efficient hash for strings when I know so little about their input format. Any thoughts as to which would be better?

EDIT:
I forgot one key aspect. I don’t know the length of the strings and so am concerned memory usage may grow too lage for long strings. If I used the hash method I would do something where after X characters the hash doesn’t hash on a per-character basis to avoid the memory consumption being too huge.

What I would really like is a hash map implementation that keeps multuple values in the ‘bucket’ sorted in an ordered manaer so it can offer a (log N) search of the buckets; but I don’t think that exists in stardrd C++ and it’s not worth writeing from scratch.

pps. the data is near-static. which I’ll occasionally have to add to the list it’s rare and I’m willing to accept a slow write time. I only care about the lookup time.

  • 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-09T19:59:48+00:00Added an answer on June 9, 2026 at 7:59 pm

    It is difficult to make a single recommendation. It depends on several tradeoffs (type of iteration, memory vs lookup). Throughout I assume that you can use a C++11 compiler (or the equivalent Boost or TR1 libraries).

    If the insertion/lookup times are the most important to you, I would definitely use std::unordered_set (see reference) with std::hash<std::string> (see reference). Both insertion and lookup are O(1) on average (amortized constant). If

    Note that the unordered hash containers do not allow you to do iteration in sorted order. So if you want sorted iteration, then you can use the ordered container std::set<std::string>, but the price you pay is O(log N) lookup/insertion.

    Memory constraints are more difficult to analyze. First, the ordered containers std::set and std::map need roughly 3 words per element overhead to maintain a tree structure that allows the ordered iteration. The unordered hash containers, however, have some spare capacity as hash containers operate very poorly on a full load factor.

    #include <iostream>
    #include <functional>
    #include <string>
    #include <unordered_set> // or <set> for ordered lookup
    
    int main()
    {
        // or std::set<std::string> for ordered lookup
        std::unordered_set<std::string> dictionary; 
    
        std::string str = "Meet the new boss...";
        dictionary.insert(str);
        auto it = dictionary.find(str);
    
        std::cout << *it << '\n';
    }
    

    Output on Ideone. If you also want to store Value alongside the std::string, then you can use a std::unordered_map<std::string, Value>, or std::map<std::string, Value> with the same hash function.

    Conclusion: it is best to measure what works best for your application, depending on the tradeoffs indicated above.

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

Sidebar

Related Questions

I need to be able to store a node set in variable and then
Users of the website need to able to store images in their area ,
I need to be able to store the current height and width of a
I am currently writing a Employee store and I need to be able to
I need to be able to store objects in javascript, and access them very
I need to be able to store a large list of ordered items in
I need to be able to store something like this: where the green is
I need to be able to store data for a php application in a
I need to be able to store a date (year/month/day) with no time component.
I am looking for a way to store key-value pairs. I need the lookup

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.