使用迭代器的递归调用崩溃
Recursive call with an iterator crashes
我正在尝试使用迭代器遍历字符串向量,但它需要提前检查一些值。我尝试使用以下代码执行此操作:
typedef std::vector<std::string>::iterator Cursor;
std::string check(Cursor& at) {
int number;
std::string ans = *at;
if (ans.empty()) {
return "NOT OK";
} else {
std::stringstream ss(ans);
ss >> number >> ans;
if (ans == "NOT OK") {
return "NOT OK";
} else if (ans == "VALUE") {
int x;
ss >> x;
std::string result("OK");
for (Cursor c = at; x > 0; ++c, --x) {
result = check(c);
}
return result;
} else {
return "OK";
}
}
}
但是这段代码在进入 for 循环时崩溃了,我不知道为什么。
int x;
ss >> x; // Here you should check whether x has the expected value.
std::string result("OK");
for (Cursor c = at; x > 0; ++c, --x) { // here you do not check whether c is within boundaries.
result = check(c);
}
// c should start from at+1, otherwise it may result in infinite recursion.
// Also, termination condition should be added.
for (Cursor c = at+1; x > 0 && c != myVec.end(); ++c, --x) {
相关文章:
- 将方法转换为调用该方法的成员函子对象会导致崩溃
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 调用 free() 有时会导致程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- Linux c++.在预加载的共享库中定义的基类的崩溃调用函数
- 使用 MS 绕道挂钩在调用钩子函数时崩溃C++
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 亲.InsertAtHead() 调用创建程序崩溃
- 重写require后调用"lua_getfield()"时崩溃
- 当从成员类调用封装的std::begin时,程序崩溃
- Shaderc在android上调用glsl到spv程序集时崩溃
- 从 C# 模块调用C++函数引发随机崩溃
- 从 C# Windows 应用程序调用 C dll 会导致 svchost.exe 崩溃
- EGL 在第一次 opengl 函数调用时崩溃
- winsock连接调用崩溃如果多个线程运行,则在一个线程中正常工作
- 当功能返回时,Linux C 系统()调用崩溃
- 使用迭代器的递归调用崩溃
- 同名类之间的共享 vtables:强制转换为基类型时,对虚拟方法的调用崩溃