gpt4 book ai didi

c++ - 尽 pipe 字符串可用,但在字符串中查找子字符串返回 -1

转载 作者:行者123 更新时间:2023-11-28 03:53:20 25 4
gpt4 key购买 nike

我又遇到了一个关于 C++ 的问题:

l 是输入线 (string.c_str()) > 变成线
pos 是开始搜索的位置
s是一个字符串(string.c_str())来寻找>成为命令

一切正常,直到 command 为“-1”。在这种情况下,尽管行包含它,但未找到字符串“-1”。我是不是漏掉了什么不对劲的东西?

代码:

bool Converter::commandAvailable(const char* l, int pos, const char* s) {

string line = l;
string command = s;
int x = line.find(command, pos);
if (x != -1) {
return true;
}
return false;
}

提前致谢!

最佳答案

这应该可以帮助您找到问题:

bool Converter::commandAvailable(const char* l, int pos, const char* s) 
{
string line = l;
string command = s;
std::cout << "INPUT" << std::endl;
std::cout << "LINE: " << line << std::endl;
std::cout << "CMD: " << command << std::endl;
std::cout << "START: " << pos << std::endl << std::endl;

std::size_t x = line.find(command, pos);

std::cout << "OUTPUT: " << x << std::endl;
if (x != std::string::npos)
{
return true;
}
return false;
}

关于c++ - 尽 pipe 字符串可用,但在字符串中查找子字符串返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4656969/

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