• swap函數 进阶探讨与实现


    相信以下這個C程序非常多人都見過啦。當時自己看 美少女战士谭浩强 写的那本书上的解释。反正我当时是没看太懂详细是什么意思。谱架啊~~~

    #include <stdio.h>
    
    void swap(int x, int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    
    int main()
    {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            swap(n, m);
            printf("%d <--> %d
    ", n, m);
        }
        return 0;
    }
    

    看结果。这个swap函数是一点用都没有啦,所以嘞,下面的程序就是帮助理解滴:

    #include <stdio.h>
    
    void swap(int x, int y)
    {
        printf("The formal parameter X's address : %d
    ", &x);
        printf("The formal parameter Y's address : %d
    ", &y);
        printf("--------------------------------------------
    ");
        int temp = x;
        x = y;
        y = temp;
        printf("The formal parameter X's address : %d
    ", &x);
        printf("The formal parameter Y's address : %d
    ", &y);
        printf("--------------------------------------------
    ");
    }
    
    int main()
    {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            printf("The actual parameter N's address : %d
    ", &n); //呢个actual 同埋formal系实參,形參唧意思。
            printf("The actual parameter M's address : %d
    ", &m);
            printf("--------------------------------------------
    ");
            swap(n, m);
            printf("%d <--> %d
    ", n, m);
        }
        return 0;
    }

    执行结果:



    从执行结果能够看到实參与形參的地址是不一样滴。所以。你改变形參内存地址存储的值不会影响到实參内存地址中存储的值。 实參与形參的结合仅仅是值传递。而不是地址传递。


    所以从这个“实參向形參传递地址”这个思路其中能够实现swap函数的功能:

    #include <stdio.h>
    
    void swap(int *x, int *y)  
    {
        int temp = *x;
        *x = *y;
        *y = temp;
    }
    
    int main()
    {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            swap(&n, &m);   //传递的是地址,所以形参與实参相应共用同一段地址。
            printf("%d <--> %d
    ", n, m);
        }
        return 0;
    }


    或者用第二种思路啦,“引用”,引用变量是已定义的变量的别名,它与这个变量共同指向同一段地址,它会随着引用变量值的改变而改变;

    #include <stdio.h>
    
    void swap(int &x, int &y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    
    int main()
    {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            swap(n, m);
            printf("%d <--> %d
    ", n, m);
        }
        return 0;
    }


    悄悄告訴你,還有一種方法啦,哈哈:

    #include <stdio.h>
    
    int swap(int &a, int &b)
    {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
    }
    
    int main()
    {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            swap(n, m);
            printf("%d <--> %d
    ", n, m);
        }
        return 0;
    }

    或者:

    void swap(int *x, int *y)
    {
        *y = *x ^ *y;
        *x = *x ^ *y;
        *y = *x ^ *y;
    }
    

    這個是運用到了二進制邏輯運算 + 每個元素的加法逆元 a ^ a = 0, 假设想好好理解一下的話,自己推導一下就能够了,挺簡單的~~

  • 相关阅读:
    ArcGIS自定义工具箱-列举损坏的数据源
    ArcGIS自定义工具箱-修复损坏的工作空间
    ArcGIS自定义工具箱-显示地图文档结构
    ArcGIS自定义工具箱-字段合并
    ArcGIS自定义工具箱-字段分割
    ArcGIS自定义工具箱-字段值部分替换
    [转载]ArcGIS Engine 中的多线程使用
    一个WebService Demo
    [GeoServer]Openlayers简单调用
    [ArcEngine]Geotransformation地理变换
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5199769.html
Copyright © 2020-2023  润新知