好的,所以我一直在寻找一种可以帮助我在子字符串中找到字符串的算法.我之前使用的代码来自一个类似的问题,但它没有这样做.
// might not be exposed publicly, but could be
int index_of(string const& haystack, int haystack_pos, string const& needle) {
// would normally use string const& for all the string parameters in this
// answer, but I've mostly stuck to the prototype you already have
// shorter local name, keep parameter name the same for interface clarity
int& h = haystack_pos;
// preconditions:
assert(0 <= h && h <= haystack.length());
if (needle.empty()) return h;
if (h == haystack.length()) …Run Code Online (Sandbox Code Playgroud)