将多个字符串传递给string :: find函数 [英] Pass multiple strings to the string::find function

查看:179
本文介绍了将多个字符串传递给string :: find函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能以某种方式传递多个字符串到字符串:: find函数?

Is it possible to somehow pass multiple strings to the string::find function?

例如,要找到一个字符串,我可以使用:

For example, to find a string I might use this:

str.find("a string");

我想做的是这样的:

str.find("a string" || "another string" || "yet another string")

并且让函数返回三个字符串中第一个出现的位置。

And have the function return the position of the first occurrence of any of the three strings.

谢谢对于任何建议。

推荐答案

不与 std :: string :: find ,但您可以使用 std :: find_if < algorithm>

Not with std::string::find, but you could use std::find_if 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";
}

这篇关于将多个字符串传递给string :: find函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆