简体   繁体   English

为什么指针即使在作为引用传递给函数之后也不会改变?

[英]Why a pointer is unmutated even after passed to a function as reference?

The function "hasPath" is given a pointer to a root of a binary tree and two integers.函数“hasPath”被赋予一个指向二叉树根的指针和两个整数。 It returns if there's a path between integer a to integer b.如果整数 a 到整数 b 之间存在路径,则返回。 I thought I can use a helper function "find" to find integer "a" in the tree and change the pointer as reference, then from the tree node of "a" to find integer b.我以为我可以使用辅助函数“find”在树中找到整数“a”并将指针更改为引用,然后从“a”的树节点找到整数b。 However, the find function didn't change the pointer that was passed by reference.然而,find 函数并没有改变通过引用传递的指针。

//helper function to find an integer in a tree whose root node is passed in as reference //helper 函数在树中查找一个整数,其根节点作为引用传入

bool find(BinaryTreeNode*&node, int a){
    if (node==nullptr){
        return false;
    } else if (node->data==a){
        return true;

    }else {
        return find(node->left, a) or find(node->right, a);

    }
}


//a function returns if there is a path between integer a and b in the 
//binary tree passed in as root node

bool hasPath(BinaryTreeNode* node, int a, int b){
    if (node==nullptr) return false;
    BinaryTreeNode*temp = node;

    return find(temp,a) //the pointer temp should be changed, but didn't
            and 
            find(temp, b);

}

Nothing in your find function assigns to the node reference, so the temp variable in hasPath is unchanged. find函数中没有任何内容分配给node引用,因此hasPathtemp变量hasPath更改。

To make this work you should change hasPath so that it returns the node you are interested in. There's no need to use a reference.要完成这项工作,您应该更改hasPath以便它返回您感兴趣的节点。无需使用引用。 For some reason newbies often overlook the fact that functions can return values.由于某些原因,新手经常忽略函数可以返回值的事实。

Change find to thisfind更改为此

BinaryTreeNode* find(BinaryTreeNode* node, int a)
{
    if (node == nullptr)
    {
        return nullptr; // return nullptr for not found
    }
    else if (node->data == a)
    {
        return node; // found the node
    }
    else
    {
        // recurse left or right
        BinaryTreeNode* temp = find(node->left, a);
        return temp ? temp : find(node->right, a);
    }
}

Then change hasPath to use the returned node然后更改hasPath以使用返回的节点

bool hasPath(BinaryTreeNode* node, int a, int b)
{
    if (node == nullptr)
        return false;
    node = find(node, a);
    if (node == nullptr)
        return false;
    node = find(node, b);
    if (node == nullptr)
        return false;
    return true;
}

BTW I'm not making any comment on the validity of your algorithm, but I believe the code above is what you were trying to implement.顺便说一句,我不会对您算法的有效性发表任何评论,但我相信上面的代码就是您想要实现的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在函数C ++中由引用传递的指针参数 - pointer parameter passed by reference in a function c++ 指向通过引用传递的指针的指针 - Pointer To Pointer passed by reference 为什么即使将const引用传递给矢量,也将其视为被值传递? - Why is a vector treated as if it was passed by value even when it is passed by const reference? 如果在模板函数中通过const引用传递,数组不会衰减到指针 - array doesn't decay to pointer if passed by const reference in a template function 如果它是一个函数争论并且正在通过引用传递值,我应该删除一个指针吗? - Should I delete a pointer if it is a function arguemente and is being passed values by reference? 将指针分配给 lambda function 计数通过引用传递的变量 - Assign a pointer to a lambda function countaining variables passed by reference 为什么通用引用不捕获函数指针? - Why a function pointer is not captured by universal reference? 为什么期望引用的 function 与指针一起工作? - Why does a function expecting a reference work with a pointer? 为什么我的头指针在链表中发生变化,甚至没有通过引用传递它? - Why my head pointer is changing in linked list,even not passing it by reference? 即使未使用指向指针的指针作为参数来调用函数,为什么仍能正常工作? - Why does this work even though the function is not called with a pointer to a pointer as a parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM