如何将一组二进制树的迭代器划分
How to delare an iterator for a set of Binary trees
当我尝试编译此代码时,我一直在遇到问题。我认为问题的起源是迭代器在集合中的声明,因此擦除操作给出了问题。汇编错误如下:
编译器给出此消息:
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
,并应编译。
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 如何将一组二进制树的迭代器划分
- 在自定义双链接列表中的自定义迭代器的取消运算符,找不到二进制操作员
- C2676 二进制"++":设置迭代器<TElement>未定义此运算符
- 二进制搜索树中按顺序遍历的复杂性(使用迭代器)
- 正确读取二进制文件并使用流迭代器
- C++二进制文件和迭代器:使用ifstreambuf_titerator实现1:1
- 模板二进制树的迭代器
- 如何使用节点在c++中为二进制搜索树编写迭代器
- 通过传递子迭代器进行二进制搜索
- c++初级二进制搜索迭代器
- 数据结构-C++,实现二进制树(长)的自定义迭代器