void change_degree(vector<int> &nodes, map<int, vector<int> > &edges, int vertex){
map<int, vector<int> >::iterator ite;
ite = edges.find(vertex);
vector<int> temp = (*ite).second;
vector<int>::iterator it;
for(it = temp.begin(); it != temp.end(); it++){
cout << *it;
if(nodes[*it + 1] > 1)
nodes[*it + 1]++;
}
}
此功能产生错误
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x09c930e0 ***
有人能告诉我为什么会这样,它意味着什么? 提前谢谢。
答案 0 :(得分:3)
嗯,我看到的一个问题是,您没有检查vertex
中是否确实找到了edges
。您可能正在取消引用您不拥有的内存。
void change_degree(vector<int> &nodes, map<int, vector<int> > &edges, int vertex){
map<int, vector<int> >::iterator ite = edges.find(vertex);
if (ite != edges.end()) { // <-- this is what you're missing
vector<int> temp = (*ite).second; // <-- this is probably where you're dying
vector<int>::iterator it;
for(it = temp.begin(); it != temp.end(); it++){
cout << *it;
if(nodes[*it + 1] > 1) // <-- you could also be crashing here
nodes[*it + 1]++;
}
}
}
下次尝试通过GDB运行您的应用,并检查您的堆栈跟踪。
编辑:另一种可能是您错误地将nodes
编入索引。检查nodes[*it + 1]
是否有效。