如何在一个字符串中搜索多个子字符串 [英] How to search a string for multiple substrings

查看:101
本文介绍了如何在一个字符串中搜索多个子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查一个短字符串是否与子字符串列表匹配.目前,我这样做如下所示(ideone上的工作代码)

I need to check a short string for matches with a list of substrings. Currently, I do this like shown below (working code on ideone)

bool ContainsMyWords(const std::wstring& input)
{
    if (std::wstring::npos != input.find(L"white"))
        return true;
    if (std::wstring::npos != input.find(L"black"))
        return true;
    if (std::wstring::npos != input.find(L"green"))
        return true;
    // ...
    return false;
}


int main() {
  std::wstring input1 = L"any text goes here";
  std::wstring input2 = L"any text goes here black";

  std::cout << "input1 " << ContainsMyWords(input1) << std::endl;
  std::cout << "input2 " << ContainsMyWords(input2) << std::endl;
  return 0;
}

我有 10-20 个需要与输入匹配的子字符串.我的目标是针对 CPU 利用率优化代码并降低平均情况下的时间复杂度.我以 10 Hz 的速率接收输入字符串,突发频率为 10 kHz(这是我所担心的).

I have 10-20 substrings that I need to match against an input. My goal is to optimize code for CPU utilization and reduce time complexity for an average case. I receive input strings at a rate of 10 Hz, with bursts to 10 kHz (which is what I am worried about).

agrep 库用C写的源代码,不知道有没有标准在 C++ 中等效.快速浏览一下,将其与我拥有的内容集成可能有点困难(但可行).

There is agrep library with source code written in C, I wonder if there is a standard equivalent in C++. From a quick look, it may be a bit difficult (but doable) to integrate it with what I have.

在 C++ 中,是否有更好的方法将输入字符串与一组预定义的子字符串进行匹配?

Is there a better way to match an input string against a set of predefined substrings in C++?

推荐答案

你可以使用一个大的 if,而不是几个 if 语句.然而,Nathan 的带有 std::any_of 的 Oliver 解决方案比这更快,当使子字符串数组static(这样它们就不会被一次又一次地重新创建),如下所示.

You could use one big if, instead of several if statements. However, Nathan's Oliver solution with std::any_of is faster than that though, when making the array of the substrings static (so that they do not get to be recreated again and again), as shown below.

bool ContainsMyWordsNathan(const std::wstring& input)
{
    // do not forget to make the array static!
    static std::wstring keywords[] = {L"white",L"black",L"green", ...};
    return std::any_of(std::begin(keywords), std::end(keywords),
      [&](const std::wstring& str){return input.find(str) != std::string::npos;});
}

<小时>

PS:如查找多个字符串匹配的算法中所述:

grep"系列以非常有效的方式实现了多字符串搜索.如果您可以将它们用作外部程序,那就去做吧.

The "grep" family implement the multi-string search in a very efficient way. If you can use them as external programs, do it.

这篇关于如何在一个字符串中搜索多个子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆