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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:51:55+00:00 2026-05-11T03:51:55+00:00

I have a list of about a hundreds unique strings in C++, I need

  • 0

I have a list of about a hundreds unique strings in C++, I need to check if a value exists in this list, but preferrably lightning fast.

I am currenly using a hash_set with std::strings (since I could not get it to work with const char*) like so:

stdext::hash_set<const std::string> _items; _items.insert('LONG_NAME_A_WITH_SOMETHING'); _items.insert('LONG_NAME_A_WITH_SOMETHING_ELSE'); _items.insert('SHORTER_NAME'); _items.insert('SHORTER_NAME_SPECIAL');  stdext::hash_set<const std::string>::const_iterator it = _items.find( 'SHORTER_NAME' ) );  if( it != _items.end() ) {    std::cout << 'item exists' << std::endl; } 

Does anybody else have a good idea for a faster search method without building a complete hashtable myself?


The list is a fixed list of strings which will not change. It contains a list of names of elements which are affected by a certain bug and should be repaired on-the-fly when opened with a newer version.

I’ve built hashtables before using Aho-Corasick but I’m not really willing to add too much complexity.


I was amazed by the number of answers. I ended up testing a few methods for their performance and ended up using a combination of kirkus and Rob K.’s answers. I had tried a binary search before but I guess I had a small bug implementing it (how hard can it be…).

The results where shocking… I thought I had a fast implementation using a hash_set… well, ends out I did not. Here’s some statistics (and the eventual code):

Random lookup of 5 existing keys and 1 non-existant key, 50.000 times

My original algorithm took on average 18,62 seconds
A lineair search took on average 2,49 seconds
A binary search took on average 0,92 seconds.
A search using a perfect hashtable generated by gperf took on average 0,51 seconds.

Here’s the code I use now:

bool searchWithBinaryLookup(const std::string& strKey) {    static const char arrItems[][NUM_ITEMS] = { /* list of items */ };     /* Binary lookup */    int low, mid, high;     low = 0;    high = NUM_ITEMS;    while( low < high ) {       mid = (low + high) / 2;       if(arrAffectedSymbols[mid] > strKey) {          high = mid;       }       else if(arrAffectedSymbols[mid] < strKey) {          low = mid + 1;       }       else {          return true;       }    }     return false; } 

NOTE: This is Microsoft VC++ so I’m not using the std::hash_set from SGI.


I did some tests this morning using gperf as VardhanDotNet suggested and this is quite a bit faster indeed.

  • 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. 2026-05-11T03:51:56+00:00Added an answer on May 11, 2026 at 3:51 am

    If your list of strings are fixed at compile time, use gperf http://www.gnu.org/software/gperf/ QUOTE: gperf is a perfect hash function generator. For a given list of strings, it produces a hash function and hash table, in form of C or C++ code, for looking up a value depending on the input string. The hash function is perfect, which means that the hash table has no collisions, and the hash table lookup needs a single string comparison only.

    The output of gperf is not governed by gpl or lgpl, afaik.

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

Sidebar

Related Questions

I have a list containing about 1000 file names to search under a directory
I have a question about list views. I hope someone knows the solution, because
I have a question about list in tcl, what is the correct way to
I have a list: [['18411971', 'kinase_2', 36], ['75910712', 'unnamed...', 160], ... about 60 entries
I have about 10 drop down list controls that get populated. Instead of copying/pasting
I have a question about removing non-alphanumeric characters from a list in R. I
I have some questions about the default values in a function parameter list Is
I have a question about how to catch the exception in the initialization list.
I know about TelephonyManager.getNeighboringCellInfo() List<NeighboringCellInfo> NeighboringList = tm.getNeighboringCellInfo(); I have AT&T. Is there a
I have a list of about 20k addresses in the US, and I would

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.