gpt4 book ai didi

c++ - string::substr 中无法解释的 out_of_range

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:36 27 4
gpt4 key购买 nike

在调用 substr 时,我遇到了一个关于 std::out_of_range 的恼人错误。准确的错误是

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr

我绝对确定 tmp_request 的长度大于 1。无论我将什么传递给 substr — 1、2 或 bodypos — 它总是会抛出该错误。我在 Unix 上使用 g++。

我唯一可以包括的有趣的事情是字符串有多个 "\r\n",包括一个 "\r\n\r\n"

在一个cpp文件中:

std::string tmp_request, outRequest;

tmp_request = SS_Twitter->readData();
outRequest = SS_Twitter->parse(tmp_request);

在另一个:

 std::string parse(const std::string &request)
{
std::map<std::string,std::string> keyval;
std::string outRequest;
if(request[0]=='P')
{
if(request.find("register")!=std::string::npos)
{ //we have a register request
size_t bodypos = request.find("username");
if(bodypos==std::string::npos)
{
HttpError(400,"Malformed HTTP POST request. Could not find key username.",request);
}
else
{
std::string body = request.substr(bodypos);
StringExplode(body,"&", "=",keyval);
outRequest = "doing stuff";
}

}

更新:

std::string request2("P\r\nregister\r\nusername=hello\r\n\r\n");

std::string body = request2.substr(4);

这会引发相同的错误。现在我知道这是完全有效和正确的代码,但它仍然会抛出错误。//删除源链接

最佳答案

我稍微修改了您的示例以减少使用的缩进量。
有 5 个“测试用例”,没有一个会导致任何问题。您能否提供一个示例请求来重现您遇到的问题。

编辑:忘记提及:如果这个示例(带有注释掉的位)没有产生该错误,那么您最好的选择是您的 StringExplode 函数中存在错误。您可以发布其来源,以获得更有帮助的建议。

编辑2:在您的 StringExplode 中,将 results[tmpKey] = tmpKey.substr(found+1); 更改为 results[tmpKey] = tmpResult[i].substr(found +1);。将 int found 更改为 size_t found,并删除所有 if (found > 0),这将修复您神秘的 out_of_range。您 substr-ing 了一个错误的字符串。为了以防万一,这里是修复代码:

void StringExplode(std::string str, std::string objseparator, std::string keyseperator,
std::map <std::string, std::string> &results)
{
size_t found;
std::vector<std::string> tmpResult;
found = str.find_first_of(objseparator);
while(found != std::string::npos)
{
tmpResult.push_back(str.substr(0,found));
str = str.substr(found+1);
found = str.find_first_of(objseparator);
}
if(str.length() > 0)
{
tmpResult.push_back(str);
}

for(size_t i = 0; i < tmpResult.size(); i++)
{
found = tmpResult[i].find_first_of(keyseperator);
while(found != std::string::npos)
{
std::string tmpKey = tmpResult[i].substr(0, found);
results[tmpKey] = tmpResult[i].substr(found+1);
found = tmpResult[i].find_first_of(keyseperator, found + results[tmpKey].size());
}

}
}

初始测试代码:

#include <iostream>
#include <map>
#include <string>

std::string parse(const std::string &request)
{
std::map<std::string,std::string> keyval;
std::string outRequest;

if(request[0] != 'P')
return outRequest;

if(request.find("register") == std::string::npos)
return outRequest;

//we have a register request
size_t bodypos = request.find("username");
if(bodypos==std::string::npos)
{
// HttpError(400,"Malformed HTTP POST request. Could not find key username.",request);
// you said HttpError returns, so here's a return
return outRequest;
}

std::string body = request.substr(bodypos);
// StringExplode(body,"&", "=",keyval);
outRequest = "doing stuff";

return outRequest;
}

int main()
{

std::string request("P\r\nregister\r\nusername=hello\r\n\r\n");
std::cout << "[" << parse(request) << "]\n";

request = "Pregisternusername=hello\r\n\r\n";
std::cout << "[" << parse(request) << "]\n";

request = "Pregisternusername=hello";
std::cout << "[" << parse(request) << "]\n";

request = "registernusername=hello";
std::cout << "[" << parse(request) << "]\n";

request = "";
std::cout << "[" << parse(request) << "]\n";

return 0;
}

这输出,可以预见:

[doing stuff]
[doing stuff]
[doing stuff]
[]
[]

关于c++ - string::substr 中无法解释的 out_of_range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2258900/

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