gpt4 book ai didi

c++ - rfind 和 << 崩溃程序

转载 作者:行者123 更新时间:2023-11-28 03:34:51 45 4
gpt4 key购买 nike

我有一个问题让一些程序员抓狂。语言是C/C++。考虑下面的代码...假设 request 是一个有效的 C++ 字符串。

string outMsg;
string trans_str = ("</TRANSACTION>");
int32_t position;

//Assign outMsg and find "</TRANSACTION>"
outMsg.assign(request);
position = outMsg.rfind(trans_str);

程序在 rfind(trans_str) 上崩溃。当使用 find(...) 和 replace(...) 时程序也会崩溃。大多数 C++ 方法似乎都有问题。该程序还会在如下行崩溃....

cout << "This is a string " << variable << "and this is the end"; //this is an example

程序通常会在第三个“<<”处崩溃,并且只会在本例中打印以下输出:

“这是一个字符串{变量}” CRASH。

这里发生了很多奇怪的事情。

包含“字符串”(在尖括号中)。

谢谢!

最佳答案

您的代码片段非常不完整,但是当我用我认为可能有意义的内容填充空白时,它对我来说运行良好......

#include <iostream>
#include <string>

int main()
{
std::string outMsg;
std::string trans_str = ("</TRANSACTION>");
int32_t position;

std::string request = "abcdefg</TRANSACTION>hijklmnop";

//Assign outMsg and find "</TRANSACTION>"
outMsg.assign(request);
position = outMsg.rfind(trans_str);

std::string variable = outMsg.substr(position, outMsg.size()-position);

std::cout << "This is a string\n" << variable << "\nand this is the end\n"; //this is an example
}

关于c++ - rfind 和 << 崩溃程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11266972/

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