如果 vector 中的任何字符串包含子字符串,是否有一种有效的方法来执行函数?
类似的东西
if(vector.contains(strstr(currentVectorElement,"substring"))) {
//do something
}
if(vector.contains(strstr(currentVectorElement,"substring2"))) {
//do something else
}
我唯一能想到的就是遍历每个字符串并检查子字符串是否存在。
最佳答案
您可以将 std::find_if
与 lambda 表达式一起使用
if(std::find_if(vec.begin(), vec.end(), [](const std::string& str) { return str.find("substring") != std::string::npos; }) != vec.end()) {
...
}
关于c++ - 检查 std::vector 中的字符串是否包含 C++ 中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438049/