使用方法:库函数法
#include<string>中包含一些处理字符串的函数,使用string库中的函数可以方便的解决此问题。
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.empty()) return 0;//使用empty函数判断字符串是否为空
int pos = haystack.find(needle); //用find函数判断字串在母串中是否存在,如果存在,返回起始位置索引,否则返回特定的数
return pos;
}
};