隣接するすべてのエントリを、そのセットを反復しながら消去するにはどうすればよいですか?私の場合、左から右に1だけ異なるものとして隣接する項目を定義するカスタムコンパレータがあります。したがって、セットstd::set<int> mySet = {1,2,3,4,5,7,9,10}
については、これらのエントリが私のコンパレータを満たすので、{1,2,3,4,5,9,10}
のエントリを削除したいと思います。 (シリーズの唯一のものは、隣接するペアの要素の1つではありませんので注意してください)隣接するエントリを繰り返して削除する方法
以下のコードでは、隣接するエントリを正しく追加することができます私は、隣接する対adjIter
とも右側無効イテレータと*std::next(adjIter)
コードのクラッシュの左側の両方を消去しようとした場合
int main() {
std::set<int> mySet = {1,2,3,4,5,7,9,10};
static const auto gPred = [](const auto& lhs, const auto& rhs) {
return rhs == lhs+1;
};
auto adjIter = mySet.begin();
std::set<int> adjacentEntries;
while ((adjIter = std::adjacent_find(adjIter, mySet.end(),
gPred)) != mySet.end()) {
adjacentEntries.insert(*adjIter);
// peek at the second entry that should be 1 greater than adjIter
adjacentEntries.insert(*std::next(adjIter));
// how do I erase both *std::next(adjIter) & adjIter
++adjIter;
}
std::cout << adjacentEntries << std::endl;
}
素敵な仕事!あなたが正しいと思っているように見える、私は数日間このアルゴリズムに苦労している – johnco3