【发布时间】:2011-03-22 11:04:10
【问题描述】:
我有一个std::set,我需要删除相似的相邻元素:
DnaSet::const_iterator next = dna_list.begin();
DnaSet::const_iterator actual = next;
++next;
while(next != dna_list.end()) // cycle over pairs, dna_list is the set
{
if (similar(*actual, *next))
{
Dna dna_temp(*actual); // copy constructor
dna_list.erase(actual); // erase the old one
do
{
dna_temp.mutate(); // change dna_temp
} while(!dna_list.insert(dna_temp).second); // insert dna_temp
}
++actual;
++next;
}
有时程序无法退出主循环。我认为当我删除dna_list 中的最后一个元素时会出现问题。完成这项任务的正确方法是什么?
【问题讨论】: