C++ 根据多个字符串数组搜索字符串

C++ 根据多个字符串数组搜索字符串,c++,C++,我有一个输入字符串,需要遍历它,看看它是否匹配某些单词。我有多个字符串数组,但不确定什么是一种有效的方法来检查所有数组中的字符串 字符串数组: string checkPlayType(string printDescription) { const string DeepPassRight[3] = {"deep" , "pass" , "right"}; const string DeepPassLeft[3] = {"deep" , "pass" , "left"};

我有一个输入字符串,需要遍历它,看看它是否匹配某些单词。我有多个字符串数组,但不确定什么是一种有效的方法来检查所有数组中的字符串

字符串数组:

 string checkPlayType(string printDescription)
{
    const string DeepPassRight[3] = {"deep" , "pass" , "right"};
    const string DeepPassLeft[3] = {"deep" , "pass" , "left"};
    const string DeepPassMiddle[3] = {"deep" , "pass" , "middle"};

    const string ShortPassRight[3] = {"short" , "pass" , "right"};
    const string ShortPassLeft[3] = {"short" , "pass" , "left"};
    const string ShortPassMiddle[3] = {"short" , "pass" , "middle"};

    //Must contain right but not pass
    const string RunRight = "right";
    //Must contain right but not pass
    const string RunLeft = "left";
    //Must contain middle but not pass      
    const string RunMiddle = "middle";

    const string FieldGoalAttempt[2] = {"field" , "goal" };
    const string Punt = "punt";

}

Sample Input: (13:55) (Shotgun) P.Manning pass incomplete short right to M.Harrison.

Assuming this is our only input...
Sample Output: 
Deep Pass Right: 0%
Deep Pass Left: 0%
Deep Pass Middle: 0%
Short Pass Right: 100%
Shor Pass Left:0%
...
..
..

您可以浏览数组,搜索存储在输入字符串中的数组中的单词。使用std函数可获得更好的性能。例如:

const string DeepPassRight[3] = {"deep" , "pass" , "right"};
    int i = 0;
    for(;i<3;i++)
    {
        string s = " ";
        s.append(DeepPassRight[i]);
        s.append(" ");
        std::size_t found = printDescription.find(s);
        if (found ==std::string::npos)
           break;

    }
    if(i == 3)
        // printDescription contains all DeepPassRight's members!
if(i== 2)
// just two words were found
const字符串DeepPassRight[3]={“deep”、“pass”、“right”};
int i=0;

对于(;i请尝试正则表达式:

if found "pass" then
    if regexp "(deep|short).*(left|right|middle)"
        Hooray!
    else if regexp "(left|right|middle).*(deep|short)"
        Hooray!
    else
        Aye, Caramba!
else
    Aye, Caramba!

您可能需要类似于:

void checkPlayType(const std::vector<std::string>& input)
{
    std::set<std::string> s;

    for (const auto& word : input) {
        s.insert(word);
    }
    const bool deep_present = s.count("deep");
    const bool pass_present = s.count("pass");
    const bool right_present = s.count("right");
    const bool left_present = s.count("left");
    // ...

    if (deep_present && pass_present && right_present) { /* increase DeepPassRight counter */}
    if (deep_present && pass_present && left_present) { /* increase DeepPassLeft counter */}
    // ...
}
void checkPlayType(const std::vector&input)
{
std::集s;
用于(常量自动和字:输入){
s、 插入(字);
}
const bool deep_present=s.count(“deep”);
const bool pass_present=s.count(“pass”);
const bool right_present=s.count(“右”);
const bool left_present=s.count(“左”);
// ...
如果(存在深密码和通过密码和右密码){/*增加深密码计数器*/}
如果(存在深密码和通过深密码和左密码){/*增加左密码计数器*/}
// ...
}

为什么要循环使用所有这些单独的字符串数组而不是单个字典?我必须确定它是什么类型的播放,然后输出播放发生的百分比。您可以给出示例输入和所需输出吗?如果您想自己编写,请看一看Boyer Moore或KMP算法。使用示例inp更新ut/output@moswald:我同意,您的编辑更容易理解(使用短路
&
而不是按位
&
)。带有
&
的版本也正确,使用的分支更少。