如何将一组二进制树的迭代器划分

How to delare an iterator for a set of Binary trees

本文关键字:二进制 迭代器 划分 一组      更新时间:2023-10-16

当我尝试编译此代码时,我一直在遇到问题。我认为问题的起源是迭代器在集合中的声明,因此擦除操作给出了问题。汇编错误如下:

编译器给出此消息:

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();
    }
}

正如您已经确定的那样,编译器抱怨it = cjt_arb.begin();不编译。它试图告诉你为什么:

错误:'operant ='没有匹配(操作数类型为'std :: __ debug :: set,int>> :: const_iterator {aka ___ __gnu_debug :: _ _ _ _ safe_iterator,int>>>>set,int>>>>>>>}'和'std :: __ debug :: set,int>>,treecode :: ordena> :: iterator {aka __gnu_gnu_debug :: _ _ safe_iterator,__ _ sece_iterator,int>>>>,int>>,treecode :: ordena>>>}'( it = cjt_arb.begin((;

它试图说的是

错误:没有有效的operator=set<BinTree<pair<string,int>>>::const_iterator分配给set<BinTree<pair<string,int>>>::iterator,因为这会丢弃const NESS。

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