I have to use write a program in which the dictionary开发者_StackOverflow should be used to check whether one string is a valid word in it. Is there any dictionary library I could use? If not, how could I construct a dictionary for query?
Thanks!
struct Dictionary {
Dictionary() {
// load _words, here's one possible implementation:
std::ifstream input ("/usr/share/dict/words");
for (std::string line; getline(input, line);) {
_words.insert(line);
}
}
bool contains(std::string const& word) const { return _words.count(word); }
std::set<std::string> _words;
};
Try using the STL set or map to store your words. As for obtaining the list of words, Google can probably help you out.
If you have the list of words in a file, you could load them up in a std:map and use the find method on that.
It's probably overkill but you can use the GNU Aspell library.
精彩评论