C++ で消去関数を使用すると問題が発生します。
私は次の構造を持っています:
typedef std::map<std::string,TreeElement> ObjMap;
class TreeElement {
public:
ObjMap::const_iterator parent;
std::vector<ObjMap::const_iterator > children;
}
現在、消去機能を使用して、親の子のリストから TreeElement を削除しようとしています。
//Remove from parent
SegmentMap::const_iterator parent = segment->second.parent;
std::vector<SegmentMap::const_iterator >::const_iterator it = parent->second.children.begin();
for(;((*it)->first != segment->first) && (it != parent->second.children.end()); it++);
parent->second.children.erase(it); //Compilation fails
これにより、コンパイル中に変換できないことを示すエラーが発生します
__gnu_cxx::__normal_iterator<const std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, TreeElement> >*, std::vector<std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, TreeElement> > > >
に
__gnu_cxx::__normal_iterator<std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, TreeElement> >*, std::vector<std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, TreeElement> > > >
これを修正する方法はありますか?const_iterator の代わりにイテレータを使用しようとしましたが、これによりコンパイルエラーが移動しました
std::vector<SegmentMap::const_iterator >::iterator it = parent->second.children.begin();
明確化: 消去関数が非定数イテレータを想定していることは知っています。TreeElement クラスの親と子の宣言を変更せずに、この非定数イテレータを作成する方法を探しています。