如果我按如下所示运行代码,那么输入就会崩溃。但是如果我不使用迭代器并使用注释部分中编写的代码(在"styles": [
"src/styles.css",
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
]
函数中),那么它运行良好并且不再崩溃。我无法理解为什么这个代码会被迭代器崩溃。
DFS()
答案 0 :(得分:0)
it
是全球性的,因此对DFS
的每次调用都使用相同的it
。所以,当你
for(it = graph[vertex].begin(); it != graph[vertex].end(); it++){
it
是"指出"在不同的vector
迭代。当DFS
返回时,调用DFS
会尝试继续使用错误的vector
并且vector
已经迭代到end()
的位置。 it
将增加超出范围,一旦发生,任何使用都是Undefined Behaviour。另外it != graph[vertex].end()
只能通过怪异事故才能成为真实因为它们都指向不同的vector
,所以循环进一步徘徊到未知领域并且最终这个错误表现出来,无论如何对于提问者来说,在崩溃中。
删除
vector<pair<int, int> >:: iterator it;
并替换
for(it = graph[vertex].begin(); it != graph[vertex].end(); it++)
带
for(auto it = graph[vertex].begin(); it != graph[vertex].end(); it++)
或
for(vector<pair<int, int> >::iterator it = graph[vertex].begin();
it != graph[vertex].end();
it++)
取决于目标C ++标准。
这只能解决最明显的问题。可能还有其他人。我没有检查过。