gpt4 book ai didi

c++ - 在字符串 C++ 中查找子字符串(在 "el"中查找 "hello")

转载 作者:太空狗 更新时间:2023-10-29 23:46:08 25 4
gpt4 key购买 nike

好的,所以我一直在寻找一种可以帮助我在子字符串中找到字符串的算法。我之前使用的代码来自 similar question但它并没有这样做。

// 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()) return -1;
if (haystack.compare(h, needle.length(), needle) == 0) {
return h;
}
return index_of(haystack, h+1, needle);
}

int index_of(string haystack, string needle) {
// sets up initial values or the "context" for the common case
return index_of(haystack, 0, needle);
}

这不会返回字符串“hello”上“el”的起始索引,我无法弄明白。

编辑:好的,让我向您展示更多代码,包括一些现实生活中的示例:我正在尝试分析一个字符串,该字符串是我要在文件系统中排序的文件的路径。输入示例是这样的:

输入:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv

当我尝试通过检测 SxxExx 的存在来解析此字符串以获取名称时,我寻找“s0”、“S0”等(我知道这不是最好的实现,我只是想看看是否它有效并稍后查看代码)。因此,当我使用该输入时,我在输出中得到的是:

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv

aux: 0p.HDTV.x264-IMMERSE.mkv

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv

aux: 1.720p.HDTV.x264-IMMERSE.mkv

input:/media/seagate/lol/Sons.of.Anarchy.S04.720p.HDTV.x264/Sons.of.Anarchy.S04E01.720p.HDTV.x264-IMMERSE.mkv

aux: 264-IMMERSE.mkv

辅助的预期输出:S04E01.720p.HDTV.x264-IMMERSE.mkv

如您所见,它只是在寻找字符串中的任何字符并停止,这也说明了多个有效的“找到”,而这些应该只是一个。

我尝试使用它的完整代码是:

bool StringWorker::isSeries(size_t &i) {

size_t found1, found2, found3, found4, found5, found6;
found1 = input->find_last_of("S0"); //tried several find functions including the
found2 = input->find_last_of("S1"); //index_of() mentioned above in the post
found3 = input->find_last_of("S2");
found4 = input->find_last_of("s0");
found5 = input->find_last_of("s1");
found6 = input->find_last_of("s2");

if (found1 != string::npos) {
if (input->size() - found1 > 6) {
string aux = input->substr(found1, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found1;
return true;
}
}
}
if (found2 != string::npos) {
if (input->size() - found2 > 6) {
string aux = input->substr(found2, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found2;
return true;
}
}
}

if (found3 != string::npos) {
if (input->size() - found3 > 6) {
string aux = input->substr(found3, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found3;
return true;
}
}

}
if (found4 != string::npos) {
if (input->size() - found4 > 6) {
string aux = input->substr(found4, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found4;
return true;
}
}

}
if (found5 != string::npos) {
if (input->size() - found5 > 6) {
string aux = input->substr(found5, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found5;
return true;
}
}

}
if (found6 != string::npos) {
if (input->size() - found6 > 6) {
string aux = input->substr(found6, input->size());
cout << "input:" << *input << endl;
cout << "aux: " << aux << endl;
if (isalpha(aux.at(0)) && isdigit(aux.at(1)) && isdigit(aux.at(2))
&& isalpha(aux.at(3)) && isdigit(aux.at(4))
&& isdigit(aux.at(5))) {
i = found6;
return true;
}
}

}

return false;

}

你能看出这里有什么问题吗?

最佳答案

为什么不使用 std::stringfind() 方法 -> link .

关于c++ - 在字符串 C++ 中查找子字符串(在 "el"中查找 "hello"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13508670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com