简体   繁体   English

C ++:在迭代器向量中使用erase

[英]C++: Using erase in a vector of iterators

I'm experiencing a problem using the erase function in C++. 我在使用C ++中的擦除功能时遇到了问题。

I have the following structure: 我有以下结构:

typedef std::map<std::string,TreeElement> ObjMap;
class TreeElement {
    public:
        ObjMap::const_iterator parent;
        std::vector<ObjMap::const_iterator > children;
}

Now I'm trying to remove a TreeElement from the list of children of its parent using the erase function. 现在我正在尝试使用erase函数从其父级子级列表中删除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

This gives an error during compilation indicating it can't convert 这在编译期间出现错误,表明它无法转换

__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> > > >

to

__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> > > >

Is there any way to fix this? 有没有什么办法解决这一问题? I tried using an iterator instead of a const_iterator but this just moved the compilation error to 我尝试使用迭代器而不是const_iterator,但这只是将编译错误移到了

std::vector<SegmentMap::const_iterator >::iterator it = parent->second.children.begin();

Clarification: I know the erase function expects a non-const iterator. 澄清:我知道erase函数需要一个非const迭代器。 I'm looking for a way to create this non-const iterator without changing the declaration of parent and children in the TreeElement class. 我正在寻找一种方法来创建这个非const迭代器,而无需更改TreeElement类中的的声明。

Parent is const iterator, therefore parent->second is const, therefore parent->second.children is const, therefore parent->second.children.begin() returns const iterator. Parent是const迭代器,因此parent->second是const,因此parent->second.children是const,因此parent->second.children.begin()返回const迭代器。

erase expects a non-const iterator. erase需要一个非const迭代器。

You cannot do erase() when using const_iterator . 使用const_iterator时不能执行erase() The purpose of const_iterator is to prohibit modification of the vector in any way, including erasing elements through it. const_iterator的目的是禁止以任何方式修改vector ,包括通过它擦除元素。 You should use simply iterator and then fix that compilation error. 您应该使用简单的iterator ,然后修复该编译错误。

Then that compilation error is because you are trying to assign a const_iterator to a non-const iterator . 然后编译错误是因为您尝试将const_iterator分配给非const iterator If you modify and make parent a non-const iterator , the error should go away. 如果修改并使parent成为非const iterator ,则错误应该消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM