简体   繁体   中英

most efficient way to check if a string is within a wordlist

I need the most efficient way to determine whether a string is in a wordlist (this is a textfile of all words).

Obviously I could create an ofstream object and loop through each line to see whether the string is present.

Is there a quicker way? Perhaps by using a map?

Thanks

To find a particular word in a list of many words, I would use a std::unordered_set , which is a hash-table by another name.

Basically:

  1. Read words from file into set.
  2. Pick a "random combination of letters".
  3. use find() to see if it's in the set.
  4. Go to 2 as required.

Obviously, if you only want to search for a single word, it's no point in loading into a set. Just read the file and check as you go (because on average you'll only need to read half the file, which is obviously about 50% faster than reading the whole file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM