调试断言失败:字符串下标超出范围
Debug Assertion Failure: string subscript out of range
我正在尝试学习编码,并正在创建一个二进制搜索程序。在尝试运行我的代码时,当我尝试调用搜索函数时,我得到一个调试断言失败(参见下面的调试输出)。我搞砸了什么?
#include<iostream>
#include<string>
using namespace std;
const int N = 100;
string names[N];
string query;
//function to search for names in the list
string search(int largestNum, int smallestNum, string names, string query);
int main(){
cout << "Please enter the names, pressing enter after each entry." << endl
<< "Type done when you are finished the list" << endl
<< "1.";
cin >> names[0];
int ticker1 = 1;
/*this loop outputs a number 1 greater then the last and allows the
user to enter a new name until they type "done" which will exit the loop*/
do {
cout << endl << ticker1 + 1 << ".";
cin >> names[ticker1];
ticker1++;
} while ((names[ticker1 - 1] != "done") && (names[ticker1 - 1] != "Done"));
cout << endl;
int ticker2 = 0;
while (ticker2 < (ticker1 - 1)){
cout << ticker2 + 1 << "." << names[ticker2] << endl;
ticker2++;
}
int largestNum = ticker1;
int smallestNum = 0;
cout << "Please Enter the name that you would like to search for:";
cin >> query;
cout << search(largestNum, smallestNum, names[N], query);
return 0;
}
//function to search for names in the list
string search(int largestNum, int smallestNum, string names, string query){
string middleName;
int middle = ((largestNum - smallestNum) / 2);
middleName = names[middle];
cout << middleName;
if (query == middleName) return { "Present", "Location:" + middle };
else return { "Not around" };
}
调试输出'Learning C++.exe' (Win32): Loaded 'C:UsersGarethProgrammingProjectsLearning C++DebugLearning C++.exe'. Symbols loaded.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64ntdll.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64kernel32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64KernelBase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcp120d.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcr120d.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64user32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64gdi32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64imm32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64msctf.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcrt.dll'. Cannot find or open the PDB file.
Debug Assertion Failed!
Program: C:WindowsSYSTEM32MSVCP120D.dll
File: c:program files (x86)microsoft visual studio 12.0vcincludexstring
Line: 1685
Expression: string subscript out of range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64uxtheme.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64HsSrv.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64winmm.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64dsound.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64version.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64advapi32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64ole32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64winmmbase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64powrprof.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64sechost.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64rpcrt4.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64combase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64cfgmgr32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64devobj.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64sspicli.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64cryptbase.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64bcryptprimitives.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64dwmapi.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64clbcatq.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64oleaut32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:Program Files (x86)DisplayFusionHooksAppHookx86_45641EBC-D115-4D9A-BB79-9CB07566629C.dll'. Module was built without symbols.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64shell32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsWinSxSx86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9200.16658_none_bf1359a245f1cd12comctl32.dll'. Cannot find or open the PDB file.
'Learning C++.exe' (Win32): Loaded 'C:WindowsSysWOW64shlwapi.dll'. Cannot find or open the PDB file.
The program '[9200] Learning C++.exe' has exited with code 3 (0x3).
似乎您希望您的函数在名称数组(names
)中找到特定名称(query
)。但是这个函数的接口是不正确的。
它的参数names
目前是string
类型,这意味着它是一个单字符串。如果你想传递全局变量names
in,修改函数以接受指向string
的指针,并使它指向数组的第一个元素:
string search(int largestNum, int smallestNum, const string *names, string query)
{
// as before..., except:
if (query == middleName) return "PresentnLocation:" + std::to_string(middle);
else return "Not around";
}
在调用点,只传递names
而不是names[N]
(无论如何都是在访问越界索引):
cout << search(largestNum, smallestNum, names, query);
原始断言的来源是,用{ "Present", "Location: " + middle }
初始化std::string
甚至不会做您可能认为它所做的事情。它调用std::string
构造函数,该构造函数接受两个迭代器,用于分隔要复制到字符串中的字符范围。
所以你实际上是在告诉程序:"从字面量"Present"
的第一个字符的地址开始,一直到字面量"Location: "
的第一个字符middle
的地址,将中间的所有内容复制到字符串中。"这当然是一团糟。
如果你需要一些学习资源,我们在Stack Overflow上保留了一些优秀的c++书籍列表。
您将names[N]
传递给search(..)
,但很可能位置100不存在。N
设置为100。只需传递一个有效的数组位置或传递整个数组和names
的长度来迭代names
。此外,您正在使用这一行middleName = names[middle];
,但names
不是字符串数组,只是string
。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围