如何为一组二叉树进行delare迭代器

时间:2019-05-08 09:48:47

标签: c++

当我尝试编译此代码时,我一直遇到问题。我认为问题的根源在于集合中迭代器的声明,因此擦除操作会产生问题。 编译错误如下:

编译器给出以下消息:

Treecode.cc: In member function ‘void Treecode::recalcular_treecode()’:
Treecode.cc:35:24: error: no match for ‘operator=’ (operand types are ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >}’ and ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >}’)
     it = cjt_arb.begin();
                        ^
In file included from /usr/include/c++/7/debug/set.h:34:0,
                 from /usr/include/c++/7/debug/set:35,
                 from /usr/include/c++/7/set:66,
                 from Treecode.hh:10,
                 from Treecode.cc:1:
/usr/include/c++/7/debug/safe_iterator.h:199:7: note: candidate: __gnu_debug::_Safe_iterator<_Iterator, _Sequence>& __gnu_debug::_Safe_iterator<_Iterator, _Sequence>::operator=(const __gnu_debug::_Safe_iterator<_Iterator, _Sequence>&) [with _Iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; _Sequence = std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       operator=(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
       ^~~~~~~~
/usr/include/c++/7/debug/safe_iterator.h:199:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >}’ to ‘const __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >&’
/usr/include/c++/7/debug/safe_iterator.h:231:7: note: candidate: __gnu_debug::_Safe_iterator<_Iterator, _Sequence>& __gnu_debug::_Safe_iterator<_Iterator, _Sequence>::operator=(__gnu_debug::_Safe_iterator<_Iterator, _Sequence>&&) [with _Iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; _Sequence = std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       operator=(_Safe_iterator&& __x) noexcept
       ^~~~~~~~
/usr/include/c++/7/debug/safe_iterator.h:231:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >}’ to ‘__gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >&&’
Treecode.cc:42:30: error: no matching function for call to ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::erase(std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator&)’
         it = cjt_arb.erase(it);
                              ^
In file included from /usr/include/c++/7/debug/set:35:0,
                 from /usr/include/c++/7/set:66,
                 from Treecode.hh:10,
                 from Treecode.cc:1:
/usr/include/c++/7/debug/set.h:346:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::iterator std::__debug::set<_Key, _Compare, _Allocator>::erase(std::__debug::set<_Key, _Compare, _Allocator>::const_iterator) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::const_iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       erase(const_iterator __position)
       ^~~~~
/usr/include/c++/7/debug/set.h:346:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >}’ to ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >}’
/usr/include/c++/7/debug/set.h:363:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::size_type std::__debug::set<_Key, _Compare, _Allocator>::erase(const key_type&) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::size_type = long unsigned int; std::__debug::set<_Key, _Compare, _Allocator>::key_type = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >]
       erase(const key_type& __x)
       ^~~~~
/usr/include/c++/7/debug/set.h:363:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >}’ to ‘const key_type& {aka const BinTree<std::pair<std::__cxx11::basic_string<char>, int> >&}’
/usr/include/c++/7/debug/set.h:378:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::iterator std::__debug::set<_Key, _Compare, _Allocator>::erase(std::__debug::set<_Key, _Compare, _Allocator>::const_iterator, std::__debug::set<_Key, _Compare, _Allocator>::const_iterator) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::const_iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       erase(const_iterator __first, const_iterator __last)
       ^~~~~
/usr/include/c++/7/debug/set.h:378:7: note:   candidate expects 2 arguments, 1 provided
Treecode.cc:45:30: error: no matching function for call to ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::erase(std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator&)’
         it = cjt_arb.erase(it);
                              ^
In file included from /usr/include/c++/7/debug/set:35:0,
                 from /usr/include/c++/7/set:66,
                 from Treecode.hh:10,
                 from Treecode.cc:1:
/usr/include/c++/7/debug/set.h:346:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::iterator std::__debug::set<_Key, _Compare, _Allocator>::erase(std::__debug::set<_Key, _Compare, _Allocator>::const_iterator) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::const_iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       erase(const_iterator __position)
       ^~~~~
/usr/include/c++/7/debug/set.h:346:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >}’ to ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena>::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >}’
/usr/include/c++/7/debug/set.h:363:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::size_type std::__debug::set<_Key, _Compare, _Allocator>::erase(const key_type&) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::size_type = long unsigned int; std::__debug::set<_Key, _Compare, _Allocator>::key_type = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >]
       erase(const key_type& __x)
       ^~~~~
/usr/include/c++/7/debug/set.h:363:7: note:   no known conversion for argument 1 from ‘std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >::const_iterator {aka __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > > >}’ to ‘const key_type& {aka const BinTree<std::pair<std::__cxx11::basic_string<char>, int> >&}’
/usr/include/c++/7/debug/set.h:378:7: note: candidate: std::__debug::set<_Key, _Compare, _Allocator>::iterator std::__debug::set<_Key, _Compare, _Allocator>::erase(std::__debug::set<_Key, _Compare, _Allocator>::const_iterator, std::__debug::set<_Key, _Compare, _Allocator>::const_iterator) [with _Key = BinTree<std::pair<std::__cxx11::basic_string<char>, int> >; _Compare = Treecode::ordena; _Allocator = std::allocator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >; std::__debug::set<_Key, _Compare, _Allocator>::const_iterator = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >, std::__debug::set<BinTree<std::pair<std::__cxx11::basic_string<char>, int> >, Treecode::ordena> >; typename std::__cxx1998::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<BinTree<std::pair<std::__cxx11::basic_string<char>, int> > >]
       erase(const_iterator __first, const_iterator __last)
       ^~~~~
/usr/include/c++/7/debug/set.h:378:7: note:   candidate expects 2 arguments, 1 provided

我尝试在下一行中解引用声明为= cjt_arb.begin()的声明,但这不起作用。

#include "Treecode.hh"
using namespace std;



void Treecode::recalcular_treecode() {

    set<BinTree<pair<string,int>>>::iterator it = cjt_arb.begin(); // Problem probably here
    if (cjt_arb.size() == 1) {tree = *it;}
    else {
        pair <string, int> a;
        pair <string, int> b;
        BinTree <pair <string,int> > left = *it;
        a = left.value();
        it = cjt_arb.erase(it);
        BinTree <pair <string,int> > right = *it;
        b = right.value();
        it = cjt_arb.erase(it);
        pair <string, int> node;
        if (a.first <b.first) {node.first = a.first + b.first;}
        else {node.first = b.first + a.first;}
        node.second = a.second + b.second;
        BinTree<pair<string,int>> unio(node,left, right);
        cjt_arb.insert(unio);
        recalcular_treecode();
    }
}

1 个答案:

答案 0 :(得分:0)

您已经知道,编译器在抱怨it = cjt_arb.begin();没有编译。它试图告诉您原因:

  

错误:“ operator =”不匹配(操作数类型为“ std :: __ debug :: set,int>>> :: const_iterator {aka __gnu_debug :: _ Safe_iterator,int>>>,std :: __ debug :: set,int>>>>}'和'std :: __ debug :: set,int>>,Treecode :: ordena> :: iterator {aka __gnu_debug :: _ Safe_iterator,int>>>,std :: __ debug :: set ,int>>,Treecode :: ordena>>}')        它= cjt_arb.begin();

它想说的是

  

错误:没有有效的operator=可以将set<BinTree<pair<string,int>>>::const_iterator分配给set<BinTree<pair<string,int>>>::iterator,因为那样会降低const的重要性。

您的cjt_arb似乎是const,因此您无法对其进行修改,因此无法获得非const的迭代器。将set<BinTree<pair<string,int>>>::iterator替换为set<BinTree<pair<string,int>>>::const_iterator或从const中删除cjt_arb,它将编译。