• C++ 函数参数中“ *&代表什么? ”


    typedef struct BitNode  
    {  
        char value;  
        BitNode *lchild,*rchild;    
    }BitNode,*BiTree;  

    void CreatTree(BitNode* &root,char *pre,int l1,int r1,char *in,int l2,int r2)  ;

    /*   *&代表什么?  //https://zhidao.baidu.com/question/2266744263935050308.html

      这是C++的语法写法,&在形参中表示“引用”实参,
    LNode * &lst ;  中LNode * 是个整体,表示变量类型是LNode类指针, &lst中的&表明引用实参,即代表实参的一个别名。   
    标准C是不支持这种写法的。

    追问

    &不是取地址符吗? 引用参数是什么意思

    追答

    &在变量定义区,表示引用,要注意它的用法,
    &在变量操作区,表示取地址符,如:

    int x=10, *p=&x ;  //这里&作用在x上, 是取地址符
    int &x  ;   //引用是C++引入的一个新特性,你要学的不是C++,则上述代码你是搞不懂的。 这里的&就表示引用。 一般这种形式会在形参中出现。

    LNode * &lst ;  中LNode * 是个整体,表示变量类型是LNode类指针, &lst中的&表明引用实参,即代表实参的一个别名。 操作引用变量就相当于操作实参变量



     */

    利用前序和中序求二叉树(源代码):

      1.  
        #include <iostream>
      2.  
        #include <cstdio>
      3.  
        #include <cstdlib>
      4.  
        #include <cstring>
      5.  
        using namespace std;;
      6.  
         
      7.  
        const int N=31;
      8.  
         
      9.  
         
      10.  
         
      11.  
        typedef struct BitNode
      12.  
        {
      13.  
        char value;
      14.  
        BitNode *lchild,*rchild;
      15.  
        }BitNode,*BiTree;
      16.  
         
      17.  
        /* *&代表什么?
      18.  
         
      19.  
        这是C++的语法写法,&在形参中表示“引用”实参,
      20.  
        LNode * &lst ; 中LNode * 是个整体,表示变量类型是LNode类指针, &lst中的&表明引用实参,即代表实参的一个别名。
      21.  
        标准C是不支持这种写法的。
      22.  
         
      23.  
        追问
      24.  
         
      25.  
        &不是取地址符吗? 引用参数是什么意思
      26.  
         
      27.  
        追答
      28.  
         
      29.  
        &在变量定义区,表示引用,要注意它的用法,
      30.  
        &在变量操作区,表示取地址符,如:
      31.  
         
      32.  
        int x=10, *p=&x ; //这里&作用在x上, 是取地址符
      33.  
        int &x ; //引用是C++引入的一个新特性,你要学的不是C++,则上述代码你是搞不懂的。 这里的&就表示引用。 一般这种形式会在形参中出现。
      34.  
         
      35.  
        LNode * &lst ; 中LNode * 是个整体,表示变量类型是LNode类指针, &lst中的&表明引用实参,即代表实参的一个别名。 操作引用变量就相当于操作实参变量
      36.  
         
      37.  
         
      38.  
         
      39.  
        */
      40.  
         
      41.  
        void CreatTree(BitNode* &root,char *pre,int l1,int r1,char *in,int l2,int r2)
      42.  
        {
      43.  
        if(l1<=r1&&l2<=r2)
      44.  
        {
      45.  
        int key=pre[l1];
      46.  
        int midIndex=-1;
      47.  
        for(int i=l2;i<=r2;i++)
      48.  
        {
      49.  
        if(in[i]==key)
      50.  
        {
      51.  
        midIndex=i;
      52.  
        break;
      53.  
        }
      54.  
        }
      55.  
        root=(BitNode *)malloc(sizeof(BitNode));
      56.  
        root->value=key;
      57.  
        root->lchild=NULL;
      58.  
        root->rchild=NULL;
      59.  
        int llen=midIndex-l2;
      60.  
        CreatTree(root->lchild, pre, l1+1, l1+llen, in, l2, midIndex-1);
      61.  
        CreatTree(root->rchild, pre, l1+llen+1, r1, in, midIndex+1, r2);
      62.  
        }
      63.  
        }
      64.  
         
      65.  
        void postOrderTraverse(BitNode *&root)
      66.  
        {
      67.  
        if(root->lchild)
      68.  
        postOrderTraverse(root->lchild);
      69.  
        if(root->rchild)
      70.  
        postOrderTraverse(root->rchild);
      71.  
        printf("%c",root->value);
      72.  
        }
      73.  
         
      74.  
        int main()
      75.  
        {
      76.  
        char pre[N],in[N];
      77.  
        while(scanf("%s",pre)!=EOF)
      78.  
        {
      79.  
        scanf("%s",in);
      80.  
        int len1=strlen(pre);
      81.  
        int len2=strlen(in);
      82.  
        BitNode *root=NULL;
      83.  
        CreatTree(root,pre,0,len1-1,in,0,len2-1);
      84.  
        postOrderTraverse(root);
      85.  
        printf(" ");
      86.  
        }
      87.  
        return 0;
      88.  
        }
  • 相关阅读:
    找出字符串中最长的对称字符串
    spark 数据倾斜的一些表现
    executor.Executor: Managed memory leak detected; size = 37247642 bytes, TID = 5
    机器学习复习笔记
    博客园如何在侧边添加目录
    markdown画图
    用hexo搭建博客
    苏州大学2005-2019复试上机真题及保研期中期末习题
    考研复试面试:计算机网络面试题整理
    python进行日期计算
  • 原文地址:https://www.cnblogs.com/h2zZhou/p/9565214.html
Copyright © 2020-2023  润新知