数据结构与算法in C&C++(1)/SWAP

来源:互联网 发布:数据联邦 编辑:程序博客网 时间:2024/05/17 03:23

交换是数据结构与算法的基石,本节通过5个方法的实现来谈谈如何实现swap。

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;        //C和C++可以混在一起,相互兼容int swap_test1(int a, int b);//1.传值int swap_test2(int *p1, int *p2);//2.传地址   使用指针交换两int类型变量的值//use pointer to exchange the value of two variables#define swap_test3(a,b,t) (t=a,a=b,b=t)//3.宏函数int swap_test4(int &a, int &b);//4.传引用//5.STL (standard template library)//template <class T> void swap_test5(T& a, T&b);int main(void){    int a = 0;    int b = 0;    int t = 0;    scanf("%d%d", &a, &b);    cout << "using value pass" << endl;    printf("Before Swap a:%d,b:%d\n", a, b);    if (swap_test1(a, b) < 0){        fprintf(stderr, "error in swap_test!!");        return -1;    }    printf("After Swap a:%d,b:%d\n\n", a, b);    cout << "using address pass" << endl;    printf("Before Swap a:%d,b:%d\n", a, b);    if (swap_test2(&a, &b) < 0){        fprintf(stderr, "error in swap_test!!");        return -1;    }    printf("After Swap a:%d,b:%d\n\n", a, b);    cout << "using macro function" << endl;    printf("Before Swap a:%d,b:%d\n", a, b);    if (swap_test3(a,b,t) < 0){        fprintf(stderr, "error in swap_test!!");        return -1;    }    printf("After Swap a:%d,b:%d\n\n", a, b);    cout << "using alias" << endl;    printf("Before Swap a:%d,b:%d\n", a, b);    if (swap_test4(a, b) < 0){        fprintf(stderr,"error in swap_test!!");        return -1;    }    printf("After Swap a:%d,b:%d\n\n", a, b);    cout << "using std::swap which is template<class T> void swap(T& a,T& b)" << endl;    printf("Before Swap a:%d,b:%d\n", a, b);    std::swap(a, b);    printf("After Swap a:%d,b:%d\n\n", a, b);    return 0;}int swap_test1(int a, int b){    int t = 0;    printf("In Function,Before Swap a:%d, b:%d\n", a, b);    t = a;    a = b;    b = t;    printf("In Function,After Swap a:%d,b:%d\n", a, b);     //打日志遇到的问题,在函数内部正常,但是函数调用完毕之后的状态会发生改变。所以有些日志要在主函数中函数调用之前和之后打。并非全部在子函数中打印。    return 0;}int swap_test2(int *p1, int *p2){    int *p1_temp = NULL;    int *p2_temp = NULL;    if (p1 == NULL || p2 == NULL){        fprintf(stderr, "pass parameter error!!");        return -1;    }    p1_temp = p1;    p2_temp = p2;    printf("In Function,Before swap  a:%d,b:%d\n",*p1,*p2);    *p1_temp += *p2_temp;    *p2_temp = *p1_temp - *p2_temp;    *p1_temp = *p1_temp - *p2_temp;    printf("In Function,After swap   a:%d,b:%d\n", *p1, *p2);} int swap_test4(int &a, int &b){    int t = 0;    printf("In Function,Before Swap a:%d, b:%d\n", a, b);    t = a;    a = b;    b = t;    printf("In Function,After Swap a:%d,b:%d\n", a, b);    return 0;}//讲解:交换---算法的起点/*    我们以C和C++为例子看看交换算法有几种表示方法:    1. void swap(int a, int b); 错误!!!值拷贝的方法无法交换变量的值..思考:请用内存四区图分析一下为什么值拷贝无法交换变量的值?    2. void swap (int *a, int *b); 指针是比较常用的函数参数    3. 宏函数方法     ————————————————————————    以上是C语言专用的方法    ————————————————————————    指针容易出错,于是C++中出现的一种新的方法,传引用(引用就是别名).    4. void swap (int &a, int &b);    //我们发现,以上4种算法都有一个问题:只能交换两个int类型数的值。    //因为C和C++是静态语言,所有变量都要有数据类型,这使的我们定义的接口没有普适性。    //对int数据类型的变量我们要编写一个接口。对float类型的变量我们又要编写一个接口,这样开发效率太低了。    //那么我们有什么好方法吗?那就是使用C++中的STL标准模板库    5. 调用STL模板库        std::swap(a,b);*/
0 0