针对多个字符串数组搜索字符串
Search a string against multiple string arrays
我有一个输入字符串,需要运行它,看看它是否与某些单词匹配。我有多个字符串数组,但不确定检查所有数组的字符串的有效方法是什么。
字符串数组:
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%
...
..
..
你可能想要类似的东西:
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 */}
// ...
}
尝试正则表达式:
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!
您可以遍历数组并搜索存储在输入字符串中的数组中的单词。使用 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
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组