为什么我在这里得到一个out_of_range例外
Why do I get an out_of_range exception here?
目前正在uni做一个项目,首先我需要取消连字符,看起来很简单,但是当我运行程序时,它有一个错误WeirdPuncProgram.exe: Microsoft C++ exception: std::out_of_range at memory location 0x004EF898
它也没有正确返回字符串值,在函数内部answer()
被更改并删除连字符,但一旦它出来,它只是原始输入。
#include <iostream>
#include <string>
using namespace std;
string answer;
string hyphonRemover(string answer)
{
string spacer = " ";
int h;
for (int i = 0; i < answer.length(); i++)
{
h = answer.find_first_of("-");
answer.replace(h, 1, spacer);
}
return answer;
}
int main()
{
cout << "Type a sentence which contains punctuation and ill remove it for you! " << endl << endl;
getline(cin, answer);
hyphonRemover(answer);
cout << answer << endl;
system("pause");
return 0;
}
hyphonRemover()
中每次使用answer
都是局部变量,而不是您上面定义的全局变量answer
。
因此,该函数将仅修改其局部变量。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 这里有一个错误:"string subscript is out of range"
- 每次调试此代码时,我都会得到"subscript out of range error"。有人可以帮我查明一个或多个错误吗?
- 为什么当一个流被打开时,它被截断了 ios::ate 和 ios::out 标志
- 包装一个使用char**[in/out]的C函数调用,以在cython中返回一个python列表
- 为什么我的程序有一个实例,其中"Vector Subscript out of Range"?
- 当我尝试运行此程序时,出现一个错误,该错误使程序停止并说"Vector subscript out of range"
- 我如何将一个ease in out函数从c++翻译成python ?
- 这个 C++ [out] 参数是指向枚举的指针,还是只是一个值
- 如何找到错误: *** './a.out' 中的错误: free(): 无效的下一个大小 (快速): 0x09767150 ***