关于实参形参调用函数时类型小问题:type * & x

neicole 2011-08-13 05:39:16
  大家好,有个小问题想问问大家,试过百度搜关键字 * &, 搜不出来,所以,在这里在拜托大家帮个小忙。

问题是这样的:
下面的代码有好些地方用到了 type * & x
如第35,36行:
void insert (const comparable & x, BinaryNode * & t) const;
void remove(const Comparable & x, BinaryNode * & t)const;
用了这个的时候,跟用 type * x 有什么不同呢?为什么要加上一个引用符呢?这个的意思是即引用又指针吗?
这个对效率有影响么?



// 416 二叉查找树类 Binary Search Tree
template <typename Compareable>
class BinarySearchTree
{
public:
BinarySearchTree();
BinarySearchTree(const BinarySearchTree & rhs);
~BinarySearchTree();

const Comparable & findMin() const;
const Comparable & findMax() const;
bool contains(const Comparable & x) const;
bool isempty() const;
void printTree() const;

void makeEmpty();
void insert( const Comparable & x);
void remove(const Comparable& x);

const BinarySearchTree & operator = ( const BinarySearchTree & rhs);

private:
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;

BinaryNode(const Comparable & theElement, BinaryNode * lt, BinaryNode *rt)
: element(theElement), left(lt), right(rt){}
};

BinaryNode *root;

void insert (const comparable & x, BinaryNode * & t) const;
void remove(const Comparable & x, BinaryNode * & t)const;
BinaryNode * findMax(BinaryNode *t) const;
BinaryNode * findMin(BinaryNode *t) const;
bool contains (const Comparable & x, BinaryNode * t) const;
void makeEmpty(BinaryNode * & t);
void printTree(BinaryNode * t) const;
BinaryNode * clone(Binary * t) const;
};

// 以下定义全部省略 BinarySearchTree <Compareable>::


// Returns true if x is found in the tree.
bool contains (const Comparable & x) const
{
return contains(x, root);
}
/**
* Internal method to test if an item is in a subtree.
* x is item to search for.
* t is the node that roots the subtree.
*/
bool contains(const Comparable & x, BinaryNode * t) const
{
if ( t == NULL) // 空表
return false;
else if ( x < t -> element)
return contains(x, t -> left);
else if ( t -> element < x)
return contains(x, t -> right);
else
return true; // Match
}


// 这里的findMin使用递归实现,而findMax使用循环实现

/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
BinaryNode * findMin(BinaryNode * t) const
{
if (t == NULL) // 空表
return NULL;
if ( t -> left == NULL)
return t;
return findMin(t -> left);
}

/**
* Internal method to find the largest item in a subtree t
* Return node containing the largest item.
*/
BinaryNode * findMax(BinaryNode * t) const
{
if ( t != NULL)
while ( t -> right != NULL)
t = t - > right;
return t;
}

// Insert x into the tree; duplicates are ignored.
void insert(const Comparable & x)
{
insert(x, root);
}
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the subtree
* Set the new root of the subtree.
*/
void insert( const Comparable & x, BinartyTreeNode * & t)
{
if ( t == NULL) // 基准条件,找到位置后插入
t = new BinaryNode( x, NULL, NULL);
else if (x < t-> element) // 比较大小,找到合适的位置
insert(x, t -> left);
else if (t -> element < x)
insert(x, t -> right);
else
; // Duplicate; do nothing
}

// Remove x from the tree. Nothing is done if x is not found.
void remove(const Comparable & x)
{
remove(x, root);
}
// 同很多数据结构一样,最困难的操作是删除
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the subtree.
* Set the new root fo the subtree.
*/
void remove (const Comparable & x, BinaryNode * & t)
{
if (t == NULL) // 空表 
return ; // Item not found; do nothing
if ( x < t -> element)
remove (x, t -> left);
else if ( t -> element < x)
remove (x, t -> right);
// 以下的这两个情况,都是已经找到跟x相等的元素的了
else if ( t -> left != NULL && t -> right != NULL) // two children
{
// 找出在x元素的结点以下的那些结点较小的一个作为元素替换
t -> element = findMax(t -> right) -> element;
// 替换工作完成后,可以继续实行递归,对其结点进行删除
// 这里将t指向的元素,替换后的元素作为实参
remove( t -> element, t -> right);
// 将t -> right指针移至最大值的位置,下次调用函数时,t即为最大值的指针
}
else
{
BinaryNode * oldNode = t;
t = ( ( t -> left != NULL) ? t -> left : t -> right) ;
delete oldNode;
}
}

/**
* Destructor for the tree
*/
~BinarySearchTree()
{
makeEmpty();
}
/**
* Internal method to make subtree empty.
*/
void makeEmpty(BinaryNode * & t)
{
if ( t != NULL );
{
makeEmpty( t -> left);
makeEmpty( t -> right);
delete t;
}
t = NULL;
}

/**
* Deep copy.
*/
const BinarySearchTree & operator = ( const BinarySearchTree & rhs)
{
if (this != &rhs)
{
makeEmpty();
root = clone (rhs.root);
}
return *this;
}
/**
* Internal method to clone subtree.
*/
BinaryNode * clone( BinaryNode *t ) const
{
if ( t == NULL)
return NULL;

return new BinaryNode(t -> element, clone (t -> left), clone (t -> right) );
}

http://blog.csdn.net/neicole/article/details/6684571


...全文
101 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
neicole 2011-08-13
  • 打赏
  • 举报
回复
谢谢大家,明白啦,结帖啦!
pamtry 2011-08-13
  • 打赏
  • 举报
回复
++
因为引用的实现其实就是指针
在使用上等同于
type * const *x

[Quote=引用 3 楼 terhack 的回复:]

效果跟type **x差不多
因为想改变type *x的值。。所以这样写
效率传指针跟引用应该是一样的
[/Quote]
2401_863541072 2011-08-13
  • 打赏
  • 举报
回复
效果跟type **x差不多
因为想改变type *x的值。。所以这样写
效率传指针跟引用应该是一样的
neicole 2011-08-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hpsmouse 的回复:]

type *& x;
x 是一个引用,它的引用对象是一个具有 type* 类型的指针。
[/Quote]

哦!不过还是有点疑问,为什么要这样引用,直接用指针的话,效果不是一样吗?
2011-08-13
  • 打赏
  • 举报
回复
type *& x;
x 是一个引用,它的引用对象是一个具有 type* 类型的指针。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧