zoukankan      html  css  js  c++  java
  • 202002XX-函数模板2

    1. #include "stdafx.h"

    2. #include "iostream"

    3. #include "cstring"

    4. #include "string"

    5. using namespace std;

    6. template <typename T>

    7. void swap1(T &a,T &b);//引用变量

    8. template <typename T>

    9. void swap(T *a,T *b,int n);//指针

    10.  

    11. const int lim=8;

    12. void show(int a[]);

    13. using namespace std;

    14. int main()

    15. {   

    16.    int i=10,j=20;

    17.    cout<<"i,j="<<i<<","<<j<<". ";

    18.    cout<<"交换后"<<". ";

    19.    swap1(i,j);

    20.    cout<<"i,j="<<i<<","<<j<<". ";

    21.  

    22.    int d1[lim]={0,7,0,4,1,7,7,6};

    23.    int d2[lim]={0,7,2,0,1,9,6,9};

    24.    show(d1);

    25.    show(d2);

    26.    swap(d1,d2,lim);

    27.    cout<<"swaped arrays: ";

    28.  

    29.    show(d1);

    30.    show(d2);

    31.    return 0;

    32. }

    33.  

    34. void show(int a[])

    35. {

    36.   using namespace std;

    37.   cout<<a[0]<<a[1]<<"/";

    38.   cout<<a[2]<<a[3]<<"/";

    39.   for(int i=4;i<lim;i++)//4,5,6,7

    40.   {

    41.     cout<<a[i];

    42.   

    43.   }

    44.   cout<<endl;

    45. }

    46.  

    47.  

    48. template <typename T>

    49. void swap1(T &a,T &b)

    50. {

    51.   T temp;

    52.   temp=a;

    53.   a=b;

    54.   b=temp;

    55.  

    56. }

    57.  

    58. template <typename T>

    59. void swap(T a[],T b[],int n)

    60. {

    61. T temp;

    62. for(int i=0;i<n;i++)

    63. {

    64.   temp=a[i];

    65.   a[i]=b[i];

    66.   b[i]=temp;

    67.  

    68. }

    69. }

    运行结果:
  • 相关阅读:
    slua
    Lua和.Net
    Python Embedded
    vtk
    CMAKE使用
    Create instance from class name string
    SLN & PROJ
    Python/C API
    魔兽世界编年史卷一
    PyCharm注册码
  • 原文地址:https://www.cnblogs.com/whcsrj/p/12928187.html
Copyright © 2011-2022 走看看