我是新来的,所以我可能不会清楚地问我的问题。但我确实需要帮助。所以我的作业是用 C++ 创建一个拼写检查器,它获取一个文本文件并将其与另一个字典文本文件进行比较。我有一段特定的代码需要解决。我创建了一个帮助函数 isValidWord,它接收包含容器 unordered_set 和字符串的字典。如果字符串与字典中的单词匹配,该函数将返回 true。我只会告诉你我到目前为止所拥有的。我的问题是字符串与库中的所有内容都不匹配,只检查字典中的一些内容。
#include <unordered_set>
#include <string>
bool isValidWord(std::unordered_set<std::string> dictionary, std::string& word) {
std::unordered_set<std::string>::iterator it;
for (it = dictionary.begin(); it != dictionary.end(); ++it) {
if (word == *it) {
return true;
}
}
return false;
}
最佳答案
有一个内置的find
method in unordered_set
您可以利用它而不是重新发明轮子。此外,通过引用传递 dictionary
以避免无意义的复制也是一个好主意。
关于c++ - 拼写检查器 C++;检查单词是否在字典文本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46682564/