可以使用 C++ 中的 std::string 库来实现这个函数。下面是一个示例实现:
char* find_nth_subs(const char* s, Uint sLen, const char* subcs, Uint subcsLen, int n, Bool r)
{
if (s == nullptr || sLen == 0 || subcs == nullptr || subcsLen == 0 || n <= 0)
{
return nullptr;
}
std::string str(s, sLen);
std::string sub(subcs, subcsLen);
int pos = -1;
if (r)
{
pos = str.rfind(sub); // find the last occurrence of sub in str
for (int i = 1; i < n; i++)
{
pos = str.rfind(sub, pos - 1); // find the previous occurrence of sub in str
if (pos == std::string::npos)
{
return nullptr;
}
}
}
else
{
pos = str.find(sub); // find the first occurrence of sub in str
for (int i = 1; i < n; i++)
{
pos = str.find(sub, pos + subcsLen); // find the next occurrence of sub in str
if (pos == std::string::npos)
{
return nullptr;
}
}
}
return s + pos;
}
这个函数接受一个字符串 s 和它的长度 sLen,一个子串 subcs 和它的长度 subcsLen,一个整数 n 和一个布尔值 r。它返回第 n 次出现子串 subcs 的位置,如果 r 为 true,则从左到右查找,否则从右到左查找。如果没有找到第 n 次出现的子串,则返回 nullptr。
请注意,这是一个示例实现,可能需要根据特定需求进行修改
c语言知识,c++还没有学