在搜索字符串时,Vector下标超出了范围
Vector subscript out of range when searching for a string
我正在打开一个文件并读取10k个名字的列表。一旦我将其放入数组(names[])中,我就需要搜索数组,并查看名称是否与输入的字符串匹配,如果匹配,则需要将这些匹配放入向量(vsFirst)中。这很简单,但是我得到了一个超出范围的向量下标。下面是这部分的代码:
bool NameSearch::FindLastNames(vector<string> &vsFirst, string n)
{
name = n;
int count = 0;
for (int i = 0; i < total; i++)
{
string holder = names[i];
string find = name;
cout << holder;
int index = holder.find_first_of(",");
if ((holder.rfind(find, index)) && holder.rfind(find, index)<= holder.length())
{
cout << "it was found";
vsFirst.push_back(holder);
bReady = true;
}
}
return bReady;
}
我做错了什么?我做了一些测试,看起来它甚至没有进入for循环。我对函数的调用是:
vector<string> lastNames;
nSearch.FindLastNames(lastNames, searchTerm);
数组中的所有名字都是lastname, firstname的形式。我知道数组正在加载名字
帮助吗?谢谢你!
要检查是否找到子字符串,您需要与std::string::npos
进行比较
if ((holder.rfind(find, index)) && holder.rfind(find, index)
if (holder.rfind(find, index) != std::string::npos && holder.rfind(find, index) != std::string::npos)
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围