传播智能STL复习(一)函数模板int,char等类型参数交换

01:函数模板int,char等类型参数交换

记录一下自己看传播智能的STL学习视频中的学习笔记!


.cpp文件

#include<iostream.h>
using namespace std;

template<typername T> //告诉编译器要泛型编程了,不要随便报错
void myswap(T &a, T &b)
{
    T c;
    c=a;
    a=b;
    b=c;
}

//函数模板调用

void main()
{
    char a = 'a'; //int
    char b = 'b'; //int 
    myswap<char>(a,b); //方法1:函数模板调用 

    //方法2:自动类型推倒
    //myswap(a,b);

    print("a:%d","b:%d\n",a,b);
    cout<<"hello world"<<endl;
    system("pause");
    return; 
}




猜你喜欢

转载自blog.csdn.net/u014252478/article/details/80259544