*&在这里做什么?如何更改它以获取代码作为 void binaryTree::insert(binaryNode *root,string value)?可能吗?
What is *& doing here? How do I change it to get a code as just void binaryTree::insert(binaryNode *root,string value)? Is it possible?
void binaryTree::insert(binaryNode *&root,string value)
{
if(root==NULL)
root = CreateNode(value);
else if( value > root->data )
insert (root->right,value);
else if( value < root->data )
insert (root->left,value);
}
binaryNode *&
表示对指向类型 binaryNode
的指针的引用。因为它是一个引用,所以此语句root = CreateNode(value);
修改root
引用的内容,而不是更改root
本身。
因此,如果您将其替换为 binaryNode *
,该语句将不起作用,因为您刚刚修改了此堆栈帧上的root
,并且当函数返回时,此变量将被销毁。
您需要将指针传递给指针:
void binaryTree::insert(binaryNode **root,string value)
{
if(*root==nullptr)
*root = CreateNode(value);
else if( value > *root->data )
*root = insert (*root->right,value);
else if( value < *root->data )
*root = insert (*root->left,value);
return root;
}
或者让函数将指针返回到调用方:
binaryNode *binaryTree::insert(binaryNode *root,string value)
{
if(root==nullptr)
root = CreateNode(value);
else if( value > root->data )
root = insert (root->right,value);
else if( value < root->data )
root = insert (root->left,value);
return root;
}
显然,在后者中,调用 insert() 会略有不同,因为您需要将结果分配给第一个参数:
someTree = binaryTree::insert(someTree, someString);
相关文章:
- 如何将分支添加到已存在的TTree:ROOT
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- ROOT中的实时更新
- 将 ROOT 与 CMake 集成
- CMake 'undefined reference' CERN-ROOT 的错误
- 链接 boost 库时"Error while loading shared libraries"引发的,除了我无法使用 root 访问权限来修复它
- mysql c ++连接器异常:用户"root"@'localhost'的访问被拒绝(使用密码:NO)&&MySQL服务器已消失
- 在没有root权限的情况下安装NTL,并在生成文件中修改其构建路径
- CMake 错误:ROOT 应构建为源外构建
- 如何在不需要 root 访问权限的情况下为应用程序中的线程设置相对线程优先级
- "sum root to leaf numbers"问题的解决方案
- 无效初始化类型的 BinaryNode *& 的非常量引用
- 默认区域设置"root"在 ICU 中如何工作?
- GLIBC_2.14 找不到,如果我不是 root 怎么办
- 以非 root 用户身份调用 mount() 系统调用
- "root"未命名类型错误
- 一种在没有 root 的情况下加载共享库的更简单方法
- 在ROOT的THistPainter类中使用CONTour选项时,如何添加色阶?
- *&在这里做什么?如何更改它以获取代码作为 void binaryTree::insert(binaryNode *root,string value)?可能吗?