执行错误访问 遍历向量
EXEC BAD ACCESS Iterating through vector
我有以下代码试图遍历一个向量。它接受一个值和两个迭代器作为参数:开始和结束。它特别失败while (start_iter != end_iter)
抛出EXC_BAD_ACCESS错误代码 1。
这是它爆炸的代码:
list<int>::iterator const
find_gt(
vector<list<int> >::iterator start_iter,
vector<list<int> >::iterator end_iter,
int value)
{
while (start_iter != end_iter)
{
if (start_iter->front() < value)
{
break;
}
++start_iter;
}
return start_iter->begin();
}
下面是调用它的代码:
// Reads a file into the adjacency list
void readfile(string const filename, vector <list<int> > adjList)
{
std::ifstream file(filename);
if (!file.fail())
{
string line;
int i = 0;
int valueToInsert;
while (file >> valueToInsert)
{
auto it = find_gt(adjList.begin(), adjList.end(), valueToInsert);
adjList[i].insert(it, valueToInsert);
i++;
}
file.close();
}
else
{
cout << "Could not open file!n";
throw std::runtime_error("Could not open file!");
}
}
int main()
{
// Vector of integer lists called adjList for adjacency list
vector <list<int> > adjList;
// Read the file contents into the adjacency list
readfile("input.txt", adjList);
return 0;
}
在你的函数中,你返回start_iter->begin()
,即使start_iter
等于end_iter
。
这是内存的越界访问。
相关文章:
- 在遍历处理程序的向量时注册和注销处理程序
- 遍历unordered_map向量
- 如何正确地推回然后遍历堆中对象的向量?
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 执行错误访问 遍历向量
- 尝试重载输出运算符时,我无法遍历对象向量
- 遍历向量与数组哪个更快?
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 遍历包含 c++ 中指针的向量
- 如何遍历地图向量
- 遍历抽象类的向量
- 使用迭代器遍历向量并修改内容
- 遍历向量<向量<对<int, int> > >
- 如何遍历特定字符串的向量
- 循环遍历向量<string>并保持每个元素"count"时出现问题
- 如何使用 c++11 语法创建 for 循环以遍历向量
- 使用 auto 遍历指针的常量向量
- 在C 中的字符串向量的字符串元素中循环遍历所有字符
- 使用另一个向量遍历向量的特定元素