C++ 将多个字符串传递给string::find函数
是否可以通过某种方式将多个字符串传递给string::find函数 例如,要查找字符串,我可以使用以下命令:C++ 将多个字符串传递给string::find函数,c++,string,find,C++,String,Find,是否可以通过某种方式将多个字符串传递给string::find函数 例如,要查找字符串,我可以使用以下命令: str.find("a string"); 我想做的是这样的: str.find("a string" || "another string" || "yet another string") 并让函数返回三个字符串中第一个出现的位置 谢谢您的建议。请多次调用find,或者从要查找的字符串构造正则表达式。C++11支持中的正则表达式。不支持std::string::find,但您可以
str.find("a string");
我想做的是这样的:
str.find("a string" || "another string" || "yet another string")
并让函数返回三个字符串中第一个出现的位置
谢谢您的建议。请多次调用
find
,或者从要查找的字符串构造正则表达式。C++11支持中的正则表达式。不支持std::string::find
,但您可以使用from
:
std::string str(“字符串”);
数组a{“一个字符串”、“另一个字符串”、“又一个字符串”};
自动it=std::查找if(开始(a),结束(a),
[&](const std::string&s)
{return str.find(s)!=std::string::npos;});
如果(it!=结束(a))
{
这是不可能的,但你能做的是:
auto is_there(std::string haystack, std::vector<std::string> needles) -> std::string::size_type {
for(auto needle : needles ){
auto pos = haystack.find(needle);
if(pos != std::string::npos){
return pos;
}
}
return std::string::npos;
}
auto在那里(std::string haystack,std::vector pines)->std::string::size\u类型{
用于(自动打捆针:打捆针){
自动位置=干草堆。查找(针);
if(pos!=std::string::npos){
返回pos;
}
}
返回std::string::npos;
}
一般的查找字符串的方法称为<代码>正则表达式> /Cord>,它是标准C++的一部分。这里的正则表达式是<代码>(a)((另一))字符串。似乎不适用于单个字符或?s和s引用?另一个测试:搜索字符串。我在寻找一个看起来像“i”的机制。(在本例中)出现在al字符串中。
auto is_there(std::string haystack, std::vector<std::string> needles) -> std::string::size_type {
for(auto needle : needles ){
auto pos = haystack.find(needle);
if(pos != std::string::npos){
return pos;
}
}
return std::string::npos;
}