jas*_*g3d 11 c++ iterator multimap
我正在写一个节点路径寻找算法.我需要在一定条件下运行多图并从中删除元素,但要继续迭代多图.下面是我的代码到目前为止,它似乎大部分时间都有效,但偶尔我在做nct_it ++时会出错.在递增迭代器之前从表中擦除迭代器指针是否安全?
std::list<SinkSourceNodeConn>::iterator it;
std::multimap<SysNode*, SysNode*>::iterator nct_it;
SysNode* found_node = NULL;
nct_it = node_conn_table.begin();
while(nct_it != node_conn_table.end()) {
// Find the node in the ever shrinking node connection table...
if(nct_it->first == parent_node)
found_node = nct_it->second;
// Remove the table entry if we have found a node
if(found_node) {
// Search for the node in the expanded list. If it's not found, add it.
bool found_the_node = false;
for(it = m_sink_source_nodes_.begin(); it != m_sink_source_nodes_.end(); it++) {
if(it->sink_source == sink_source && it->node == found_node)
found_the_node = true;
}
if(!found_the_node) {
recursion_list.push_back(found_node);
recursion_list.unique();
SinkSourceNodeConn ssnc;
ssnc.node = found_node;
ssnc.sink_source = sink_source;
m_sink_source_nodes_.push_back(ssnc);
if(found_node->GetPotential() < sink_source->GetPotential())
found_node->SetPotential(sink_source->GetPotential());
}
found_node = NULL; // Unset the found node...
node_conn_table.erase(nct_it);
nct_it++;
} else
nct_it++;
}
Run Code Online (Sandbox Code Playgroud)
jro*_*rok 27
在递增迭代器之前从表中擦除迭代器指针是否安全?
不,erase会使迭代器无效,之后你不应该增加它.
要正确执行此操作,请使用erase- 最后一个删除元素后面的迭代器的返回值:
std::multimap<int, int> m;
for (auto it = m.begin(); it != m.end(); ) {
if (condition)
it = m.erase(it);
else
++it;
}
Run Code Online (Sandbox Code Playgroud)
在C++ 03中,erase什么都不返回,所以你必须通过保存迭代器的副本并在删除原始文件之前递增它来手动完成:
std::multimap<int, int> m;
typedef std::multimap<int, int>::iterator Iter;¸
for (Iter it = m.begin(); it != m.end(); ) {
if ( /* some condition */ ) {
Iter save = it;
++save;
m.erase(it);
it = save;
} else
++it;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8157 次 |
| 最近记录: |