是否可以以某种方式将多个字符串传递给 string::find 函数?
例如,要查找字符串,我可以使用以下命令:
str.find("a string");
我想做的是这样的:
str.find("a string" || "another string" || "yet another string")
并让函数返回三个字符串中任何一个字符串第一次出现的位置。
感谢您的任何建议。
不与std::string::find
,但你可以使用std::find_if http://en.cppreference.com/w/cpp/algorithm/find from <algorithm>
:
std::string str("a string");
std::array<std::string, 3> a{"a string", "another string", "yet another string"};
auto it = std::find_if(begin(a), end(a),
[&](const std::string& s)
{return str.find(s) != std::string::npos; });
if (it != end(a))
{
std::cout << "found";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)