调试断言失败:表达式:字符串下标超出范围
Debug Assertion Failure : expression : string subcript out of range
我使用的是VS 2013 Ultimate这里我的代码程序声明它有问题
string word_filter(string word){
for (int i = 0; i < word.length(); i++)
cout << word[i] << " ";
string result;
char tmp;
char ch1 = word[0], ch2 = word[1], ch3 = word[3];
if (alphabetic_order(ch1) == 37 && alphabetic_order(ch2) == 37)
return " ";
int i = 0;
while (i < word.length()){
if (alphabetic_order(word[i]) != 37) {
tmp = word[i];
result += tmp;
}
++i;
}
return result;
}
请帮帮我!
看起来您正在传递一个少于四个字符的word
,并且没有检查最小长度:
char ch1 = word[0], ch2 = word[1], ch3 = word[3];
上面的代码要求至少四个字符的单词,但是在代码中没有任何地方检查word.length() > 3
对不起,这可能是问题所在
ch1[0] =词,ch2 = [1], ch3 =词[3];
检查长度或在Word中输入至少4个字符的数组,否则这行代码将失败
最好按照以下方式重写代码-
string word_filter(string word){
for (int i = 0; i < word.length(); i++)
cout << word[i] << " ";
string result;
char tmp;
char* ch = new char[word.length()];
for(int i=0;i<word.size();i++)
{
ch[i] = word[i];
}
if (word.size()=>2)
{
if (alphabetic_order(ch[0]) == 37 && alphabetic_order(ch[1]) == 37)
return " ";
}
int i = 0;
while (i < word.length()){
if (alphabetic_order(word[i]) != 37) {
tmp = word[i];
result += tmp;
}
++i;
}
return result;
}
我认为这将使你的代码更易读。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围