Searching fast through a sorted list of strings in C++

You could try a PATRICIA Trie if none of the standard containers meet your needs.

Worst-case lookup is bounded by the length of the string you're looking up. Also, strings share common prefixes so it is really easy on memory.So if you have lots of relatively short strings this could be beneficial.

Check it out here.

Note: PATRICIA = Practical Algorithm to Retrieve Information Coded in Alphanumeric


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.

Tags:

C++

Visual C++