问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求助……关于iterator

发布网友 发布时间:2022-07-22 11:41

我来回答

1个回答

热心网友 时间:2023-11-10 21:41

#ifdef __BORLANDC__
// suppress the warning message that functions containing for are not
// expanded inline
#pragma warn -8027
#endif // __BORLANDC__

class iterator;
class const_iterator;
// declare the iterator classes so the names are available

friend class iterator;
friend class const_iterator;
// allow the iterator classes to access the private section
// of BinSearchTree

class iterator
{
friend class BinSearchTree<T>;
friend class const_iterator;

public:

// constructor
iterator ()
{}

// comparison operators. just compare node pointers
bool operator== (const iterator& rhs) const
{
return nodePtr == rhs.nodePtr;
}

bool operator!= (const iterator& rhs) const
{
return nodePtr != rhs.nodePtr;
}

// dereference operator. return a reference to
// the value pointed to by nodePtr
T& operator* () const
{
if (nodePtr == NULL)
throw
referenceError("BinSearchTree iterator operator* (): NULL reference");

return nodePtr->data;
}

// preincrement. move forward to next larger value
iterator& operator++ ()
{
BSTNode<T> *p;

if (nodePtr == NULL)
{
// ++ from end(). get the root of the tree
nodePtr = tree->root;

// error! ++ requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator++ (): tree empty");

// move to the smallest value in the tree,
// which is the first node inorder
while (nodePtr->left != NULL)
nodePtr = nodePtr->left;
}
else
if (nodePtr->right != NULL)
{
// successor is the furthest left node of
// right subtree
nodePtr = nodePtr->right;

while (nodePtr->left != NULL)
nodePtr = nodePtr->left;
}
else
{
// have already processed the left subtree, and
// there is no right subtree. move up the tree,
// looking for a parent for which nodePtr is a left child,
// stopping if the parent becomes NULL. a non-NULL parent
// is the successor. if parent is NULL, the original node
// was the last node inorder, and its successor
// is the end of the list
p = nodePtr->parent;

while (p != NULL && nodePtr == p->right)
{
nodePtr = p;
p = p->parent;
}

// if we were previously at the right-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;
}

return *this;
}

// postincrement
iterator operator++ (int)
{
// save current iterator
iterator tmp = *this;

// move myself forward to the next tree node
++*this;

// return the previous value
return tmp;
}

// predecrement. move backward to largest value < current value
iterator& operator-- ()
{
BSTNode<T> *p;

if (nodePtr == NULL)
{
// -- from end(). get the root of the tree
nodePtr = tree->root;

// error! -- requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator--: tree empty");

// move to the largest value in the tree,
// which is the last node inorder
while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
} else if (nodePtr->left != NULL)
{
// must have gotten here by processing all the nodes
// on the left branch. predecessor is the farthest
// right node of the left subtree
nodePtr = nodePtr->left;

while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
}
else
{
// must have gotten here by going right and then
// far left. move up the tree, looking for a parent
// for which nodePtr is a right child, stopping if the
// parent becomes NULL. a non-NULL parent is the
// predecessor. if parent is NULL, the original node
// was the first node inorder, and its predecessor
// is the end of the list
p = nodePtr->parent;
while (p != NULL && nodePtr == p->left)
{
nodePtr = p;
p = p->parent;
}

// if we were previously at the left-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;
}

return *this;
}

// postdecrement
iterator operator-- (int)
{
// save current iterator
iterator tmp = *this;

// move myself backward to the previous tree node
--*this;

// return the previous value
return tmp;
}

private:

// nodePtr is the current location in the tree. we can move
// freely about the tree using left, right, and parent.
// tree is the address of the BinSearchTree object associated
// with this iterator. it is used only to access the
// root pointer, which is needed for ++ and --
// when the iterator value is end()
BSTNode<T> *nodePtr;
BinSearchTree<T> *tree;

// used to construct an iterator return value from
// an BSTNode pointer
iterator (BSTNode<T> *p, BinSearchTree<T> *t) : nodePtr(p), tree(t)
{}

};

class const_iterator
{
friend class BinSearchTree<T>;

public:

// constructor
const_iterator ()
{}

// used to convert a const iterator to a const_iterator
const_iterator (const iterator& pos): nodePtr(pos.nodePtr)
{}

// comparison operators. just compare node pointers
bool operator== (const const_iterator& rhs) const
{
return nodePtr == rhs.nodePtr;
}

bool operator!= (const const_iterator& rhs) const
{
return nodePtr != rhs.nodePtr;
}

// dereference operator. return a reference to
// the value pointed to by nodePtr
const T& operator* () const
{
if (nodePtr == NULL)
throw
referenceError("BinSearchTree const_iterator operator* (): NULL reference");

return nodePtr->data;
}

// preincrement. move forward to next larger value
const_iterator& operator++ ()
{
BSTNode<T> *p;

if (nodePtr == NULL)
{
// ++ from end(). get the root of the tree
nodePtr = tree->root;

// error! ++ requested for an empty tree
if (nodePtr == NULL)
throw underflowError("BinSearchTree const_iterator operator++ (): tree empty");

// move to the smallest value in the tree,
// which is the first node inorder
while (nodePtr->left != NULL)
nodePtr = nodePtr->left;
}
else
if (nodePtr->right != NULL)
{
// successor is the furthest left node of
// right subtree
nodePtr = nodePtr->right;

while (nodePtr->left != NULL)
nodePtr = nodePtr->left;
}
else
{
// have already processed the left subtree, and
// there is no right subtree. move up the tree,
// looking for a parent for which nodePtr is a left child,
// stopping if the parent becomes NULL. a non-NULL parent
// is the successor. if parent is NULL, the original node
// was the last node inorder, and its successor
// is the end of the list
p = nodePtr->parent;

while (p != NULL && nodePtr == p->right)
{
nodePtr = p;
p = p->parent;
}

// if we were previously at the right-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;
}

return *this;
}

// postincrement
const_iterator operator++ (int)
{
// save current const_iterator
const_iterator tmp = *this;

// move myself forward to the next tree node
++*this;

// return the previous value
return tmp;
}

// predecrement. move backward to largest value < current value
const_iterator& operator-- ()
{
BSTNode<T> *p;

if (nodePtr == NULL)
{
// -- from end(). get the root of the tree
nodePtr = tree->root;

// error! -- requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator--: tree empty");

// move to the largest value in the tree,
// which is the last node inorder
while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
} else if (nodePtr->left != NULL)
{
// must have gotten here by processing all the nodes
// on the left branch. predecessor is the farthest
// right node of the left subtree
nodePtr = nodePtr->left;

while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
}
else
{
// must have gotten here by going right and then
// far left. move up the tree, looking for a parent
// for which nodePtr is a right child, stopping if the
// parent becomes NULL. a non-NULL parent is the
// predecessor. if parent is NULL, the original node
// was the first node inorder, and its predecessor
// is the end of the list
p = nodePtr->parent;
while (p != NULL && nodePtr == p->left)
{
nodePtr = p;
p = p->parent;
}

// if we were previously at the left-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;
}

return *this;
}

// postdecrement
const_iterator operator-- (int)
{
// save current const_iterator
const_iterator tmp = *this;

// move myself backward to the previous tree node
--*this;

// return the previous value
return tmp;
}

private:

// nodePtr is the current location in the tree. we can move
// freely about the tree using left, right, and parent.
// tree is the address of the BinSearchTree object associated
// with this iterator. it is used only to access the
// root pointer, which is needed for ++ and --
// when the iterator value is end()
const BSTNode<T> *nodePtr;
const BinSearchTree<T> *tree;

// used to construct a const_iterator return value from
// an BSTNode pointer
const_iterator (const BSTNode<T> *p, const BinSearchTree<T> *t) : nodePtr(p), tree(t)
{}

};

使用类似的方法,使你的迭代器符合输出迭代器的概念就可以了。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 黑客攻击报案有用吗 一段关于iterator的java小程序,看看错在哪 C++ 关于iterator的问题 预防血小板减少性紫癜应该怎样做? 也将邀请专业人士做产品的详细介绍 英语翻译 2022年二月提交辞职报告,厂方三月底才抛出,导制三月份不能交怎么办 分别in,on,at在street的用法 求一篇关于IT方面的英文演讲的PPT。。。 IT产品营销专业的毕业论文选题有哪些 关于IT业论文参考文献 求“论当代中国的IT行业发展”的论文 求一个关于IT的短篇文章,我们TEAM周会要讲,大概能让我说上个15-20分钟左右 求一篇文章,描写一位IT经理人在中关村的故事 it行业之我见的论文 求一篇关于IT的文章 EXCEL数据表怎么显示次刻度数值 谁能提供it巨头的图片(ati.inter.amd) 求IT狂人里出现的一张海报的原图和出处 谁有有关IT的很多图片 求几张简单图片,有关学校、娱乐圈、IT~~ 求绿箭侠里it女的高清图片 Iterator i = c.iterator() 关于这句话的问题 关于Iterator ITP的主要发病机制 下列支持ITP诊断的是 ITP发病的相关因素是 有关文学社活动的互动游戏 关于itunes和iphone的问题,急!! 关于itunes、iPhone的问题 急 关于iphone和itunes问题!!急!!! 当你参加一个高度依赖信息系统的企业的财务报表审计,你会从哪些方面着手开展IT审计工作?_百度问一问 iphone和itunes的问题,很急!! 关于iphone与itunes的问题。求好心人!!! 一篇外文翻译,关于IT审计。求中文 关于iphone与itunes的问题 关于IT审计,有什么证书吗? 关于iTunes与iphone的使用问题。 关于itunes与iphone连接的问题! 关于iTunes和iPhone的几个问题 it作形式宾语的句型? 关于it作形式宾语的句子