开发者

dictionary library in C++

开发者 https://www.devze.com 2022-12-19 02:51 出处:网络
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 cou

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号