Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何判断一个字符串是否包含在另一个字符串中_C++_String - Fatal编程技术网

C++ 如何判断一个字符串是否包含在另一个字符串中

C++ 如何判断一个字符串是否包含在另一个字符串中,c++,string,C++,String,C++代码。 例子: x:“这是我的第一个节目”; y:“我的” 您可以使用执行此操作。您可以使用std::string::find() 函数的编写方式取决于对空字符串的搜索是否成功。 如果考虑在任何字符串中存在空字符串,则函数将看起来为 bool function( const std::string &x, const std::string string &y ) { return ( x.find( y ) != std::string::npos ); } 如

C++代码。 例子: x:“这是我的第一个节目”; y:“我的”


您可以使用

执行此操作。您可以使用
std::string::find()


函数的编写方式取决于对空字符串的搜索是否成功。 如果考虑在任何字符串中存在空字符串,则函数将看起来为

bool function( const std::string &x, const std::string string &y )
{
    return ( x.find( y ) != std::string::npos );
}

如果考虑空字符串的搜索返回false,则函数将看起来为

bool function( const std::string &x, const std::string string &y )
{
    return ( !y.empty() && x.find( y ) != std::string::npos );
}

我希望函数对空字符串返回false。

std::search
,这将适用于任何序列。堆栈溢出不是神奇的代码生成器。
bool function( const std::string &x, const std::string string &y )
{
    return ( x.find( y ) != std::string::npos );
}
bool function( const std::string &x, const std::string string &y )
{
    return ( !y.empty() && x.find( y ) != std::string::npos );
}