c++ - 忽略几个不同的词.. c++?

标签 c++ string

我正在阅读多个文档,并为我阅读的单词编制索引。但是,我想忽略常见的大小写单词(a、an、the、and、is、or、are 等)。

这样做有捷径吗?不仅仅是做...

if(word=="and"|| word=="is"|| etc etc....) 忽略单词;

例如,我能否以某种方式将它们放入 const 字符串中,并让它只检查字符串?不确定...谢谢!

最佳答案

创建一个 set<string> 用你想排除的词,然后使用 mySet.count(word)以确定单词是否在集合中。如果是,计数将为 1 ;它将是0否则。

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
    const char *words[] = {"a", "an", "the"};
    set<string> wordSet(words, words+3);
    cerr << wordSet.count("the") << endl;
    cerr << wordSet.count("quick") << endl;
    return 0;
}

关于c++ - 忽略几个不同的词.. c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158633/

相关文章:

c++ - 给定一个字符串,每当你看到单词 "ant"时,将单词 "termite"替换为字符串

java - 查找字符串中所有用点分隔的子字符串?

c++ - 弃用的转换错误

c++ - 如何在 OpenGL 3.3 中实现 3d 纹理

c++ - 自动打印GDB中最后一个表达式的值,如VisualStudio的 "Auto"watch window

c++ - Linux 和 Windows c++ 上的低级文件操作处理

string - 戈朗 : read text file line by line of int strings

c# - 将文件过滤器应用于文件名的字符串 [],而不打开 OpenFileDialog

c++ - 将参数传递给在 C++ 中不起作用的可执行文件

c++ - std::inserter 不工作