我需要为单词列表实现一个搜索机制,并给出是否找到的单词不在提供的文本中的结果。
我使用 boost
库实现了这个。如下所示,
int main(int argc, char**argv)
{
int count = argc - 2;
std::string text = argv[1];
for(int i = 2; i < argc; i++)
{
boost::regex re(argv[i], boost::regex::icase);
if(boost::regex_search(text, re))
{
std::cout<<re<<" Found in "<<text<<std::endl;
count--;
}
else std::cout<<re<<" Not Found in " <<text<<std::endl;
}
if(count == 0)
{
std::cout <<"ALL WORDS ARE FOUND"<<std::endl;
}
else std::cout << "SOME WORDS ARE NOT FOUND"<<std::endl;
}
它在命令行参数上运行良好。
现在,我需要的是我应该直接向 boost search
方法(如果有的话)提供一个单词数组作为参数,该参数应该返回是否找到所有单词。
我用 Google 搜索过这个但没用。
最佳答案
要么只使用一个数组:
std::vector<std::string> arr { "many", "words", "in", "array" };
int count = arr.size();
std::string text = argv[1];
for(std::string const& word : arr)
{
boost::regex re(text, boost::regex::icase);
if(boost::regex_search(text, re))
{
std::cout<<re<<" Found in "<<text<<std::endl;
count--;
}
else std::cout<<re<<" Not Found in " <<text<<std::endl;
}
您可以创建一个包含所有要查找的单词的正则表达式(这会(大大)降低运行时的复杂性,但会增加计算单个单词出现的难度)。
关于c++ - 是否有像boost库中的boost::regex_search那样通过将数组作为任意参数来搜索单词数组的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863583/