C++模板(一)

c++的一种编程思想:泛型编程 主要利用技术模板

c++提供两种模板机制:函数模板和类模板

一、函数模板

1.1函数模板语法

函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体确定,用一个虚拟的类型替代

语法:

template<typename T> //typename可以用class替代

函数声明或者定义

解释:

template---声明创建模板

typename---表明后面的符号是一种数据类型可以用class代替

T---通用的数据类型,名称可以替换,通常为大写字母

例子:

#include <iostream>
using namespace std;
 
template<typename T>
void my_swap(T &a, T &b)
{
        T  temp = a;
        a  = b;
        b  = temp;
}
 
void test01()
{
        int a =  10;
        int b =  20;
        int c =  12;
        int d =  21;
        //使用函数模板有两种方式
        //1.自动类型推导
        my_swap(a, b);
        cout  << "a = " << a << endl;
        cout  << "b = " << b << endl;
        //显示指定类型
        my_swap<int>(c, d);
        cout  << "c = " << c << endl;
        cout  << "d = " << d << endl;
        
}
int main()
{
        
        test01();
        return 0;
}
<<
a = 20
b = 10
c = 21
d = 12

1.2函数模板注意事项

1)自动类型推导,必须推导出一致的数据类型T,才可以使用

2)模板必须要确定T的数据类型,才可以使用

#include <iostream>
using namespace std;
 
 
template<typename T>
void test01()
{
        cout  <<  "test01()的调用" << endl;
}
int main()
{
        
        test01<int>();
        return 0;
}
<<test01()的调用

1.3函数模板案例

#include <iostream>
using namespace std;
 
 
//交换模板
template<class  T>
void my_swap(T &a, T &b)
{
        T  temp = a;
        a  = b;
        b  = temp;
}
 
//选择排序模板
template<class  T>
void sort_Array(T Arr[], int  length)
{
        for(int i = 0; i < length; i ++)
        {
                int max = i;
                for(int j = i; j < length; j ++)
                {
                        if(Arr[j]  > Arr[max])
                        {
                                max  = j;
                        }
                }
                //交换元素
                my_swap(Arr[max],  Arr[i]);
        }
}
 
 
//打印模板
template<class  T>
void printArray(T Arr[], int  length)
{
        for(int i = 0; i < length; i ++)
        {
                cout  << Arr[i] << " ";
        }
        cout  << endl;
}
 
void test01()
{
        char a[]  = "abcdefghijk";
        int length = sizeof(a)  / sizeof(char);
        sort_Array(a, length);
        printArray(a, length);
}
 
void test02()
{
        int b[]  = {22, 34,  13, 56,  -34, 343,  5940};
        int length = sizeof(b)  / sizeof(int);
        sort_Array(b, length);
        printArray(b, length);
}
int main()
{
        test01();
        test02();
        return 0;
}
<<
k j i h g f e d c ba 
5940 343 56 34 22 13-34 

1.4普通函数与函数模板区别

1)普通函数调用可以发生自动转换

2)函数模板调用时,如果利用自动类型推导不会发生隐式类型转换

3)如果利用显示指定类型的方式,可以发生隐式转换类型

#include <iostream>
using namespace std;
 
 
//ordinary function
int my_add(int a,  int b)
{
        return a + b;
}
 
//function template
template<class  T>
T myadd2(T a, T  b)
{
        return a + b;
}
 
 
void test01()
{
        int a =  12;
        char c =  'c';
        cout  << my_add(a, c) << endl;
}
 
 
// void test02()
// {
//          int a = 12;
//          char c = 'c';
//          cout << myadd2(a, c)  << endl;
        
// }
 
void test03()
{
        int a =  12;
        char c =  'c';
        cout  << myadd2<int>(a, c) << endl;
        
}
int main()
{
        test01();
        //test02();
        test03();
        return 0;
}
<<
111
111

1.5普通函数与函数模板调用规则

1.5.1函数模板与普通函数都能实现时,优先调用普通函数

#include <iostream>
using namespace std;
 
 
void myprint(int a,  int b)
{
        cout  <<  "普通函数调用" << endl;
}
 
template<class  T>
void myprint(T a, T  b)
{
        cout  <<  "函数模板调用" << endl;
}
 
void test01()
{
        int a =  10;
        int b =  20;
        myprint(a, b);
}
int main()
{
        test01();
        return 0;
}
<<
普通函数调用

1.5.2可以通过空模板参数列表来强制调用函数模板

#include <iostream>
using namespace std;
 
 
void myprint(int a,  int b)
{
        cout  <<  "普通函数调用" << endl;
}
 
template<class  T>
void myprint(T a, T  b)
{
        cout  <<  "函数模板调用" << endl;
}
 
void test01()
{
        int a =  10;
        int b =  20;
        myprint<>(a, b);
}
int main()
{
        test01();
        return 0;
}
<<
函数模板调用

1.5.3函数模板也可以发生重载

与普通函数重载类似

1.5.4如果函数模板可以产生更好的匹配,优先调用函数模板

#include <iostream>
using namespace std;
 
 
void myprint(int a,  int b)
{
        cout  <<  "普通函数调用" << endl;
}
 
template<class  T>
void myprint(T a, T  b)
{
        cout  <<  "函数模板调用" << endl;
}
 
void test01()
{
        char a =  'a';
        char b =  'b';
        myprint(a, b);
}
int main()
{
        test01();
        return 0;
}
<< 函数模板调用

Ps:在实际开发中,尽量不要让它们同时出现

1.6模板局限性

模板对于一些特定数据类型,需要具体化做特殊实现

#include <iostream>
using namespace std;
 
class Person
{
public:
        Person(string name, int age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        string  m_name;
        int m_age;
 
};
 
template<class  T>
bool mycompare(T &a, T &b)
{
        if(a == b)
        {
                return true;
        }
        else
        {
                return false;
        }
}
 
//利用具体化Person版本实现代码,具体化优先调用
template<> bool mycompare(Person &p1, Person &p2)
{
        if(p1.m_name == p2.m_name  && p1.m_age  == p2.m_age)
        {
                return true;
        }
        else
        {
                return false;
        }
}
 
void test01()
{
        Person  p1("tom", 23);
        Person  p2("tom", 22);
        bool reg =  mycompare(p1, p2);
        if(reg  == true)
        {
                cout  << "p1 == p2" << endl;
        }
        else
        {
                cout  << "p1 != p2" << endl;
        }
}
 
void test02()
{
        int a =  10;
        int b =  10;
        bool reg =  mycompare(a, b);
        if(reg  == true)
        {
                cout  << "a == b" << endl;
        }
        else
        {
                cout  << "a != b" << endl;
        }
}
int main()
{
        test01();
        test02();
        return 0;
}
<<
p1 != p2
a == b

二、类模板

2.1类模板语法

类模板作用:建立一个类,类中的成员 数据类型可以不具体制定,用一个虚拟的类型来代表

语法:

template<typename T>

解释:

template---声明创建模板

typename---表明后面的符号是一种数据类型可以用class代替

T---通用的数据类型,名称可以替换,通常为大写字母

示例:

#include <iostream>
using namespace std;
#include <string>
 
template<class  NameType, class AgeType>
class Person
{
public:
        Person(NameType name, AgeType age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        void Person_show()
        {
                cout  <<  "姓名: " << this->m_name << " 年龄: " << this->m_age << endl;
        }
 
        NameType  m_name;
        AgeType  m_age;
 
};
 
void test01()
{
        Person<string,  int> p1("Tom", 23);
        p1.Person_show();
 
}
 
int main()
{
        test01();
        return 0;
}
<<
姓名: Tom 年龄: 23

2.2类模板与函数模板区别

//1)类模板没有自动推导类型使用方式
//2)类模板在模板参数可以有参数
#include <iostream>
using namespace std;
#include <string>
 
template<class  NameType, class AgeType = int>
class Person
{
public:
        Person(NameType name, AgeType age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        void Person_show()
        {
                cout  <<  "姓名: " << this->m_name << " 年龄: " << this->m_age << endl;
        }
 
        NameType  m_name;
        AgeType  m_age;
 
};
 
void test01()
{
        Person<string, int> p1("猴哥", 1000);
        p1.Person_show();
        Person<string> p2("八戒", 999);
        p2.Person_show();
 
}
 
int main()
{
        test01();
        return 0;
}
<<
姓名:猴哥 年龄: 1000
姓名:八戒 年龄: 999

2.3类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

普通类的成员函数一开始就可以创建

类模板中成员函数在调用时才会创建

#include <iostream>
using namespace std;
 
 
class Person1
{
public:
        void show_Person1()
        {
                cout  << "Person1 show" << endl;
        }
};
 
class Person2
{
public:
        void show_Person2()
        {
                cout  << "Person2 show" << endl;
        }
};
 
template<class  T>
class MyClass
{
public:
        T  obj;
        void fun1()
        {
                obj.show_Person1();
        }
 
        void fun2()
        {
                obj.show_Person2();
        }
};
 
 
void test01()
{
        MyClass<Person1>m;
        m.fun1();
}
 
int main()
{
        test01();
        return 0;
}
<<
Person1 show

这里只是将模板参数定义为了Person1,但是并没有把Person2加入,事实上如果调用m.fun2()才会报错,这说明了类模板中的成员函数只有在调用才会创建

2.4类模板对象做函数参数

类模板实例化出的对象,向函数传参的方式

2.4.1指定传入类型---直接显示对象的数据类型

#include <iostream>
using namespace std;
#include <string>
 
 
template<class  T1, class T2>
class Person
{
public:
        Person(T1 name, T2 age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        T1  m_name;
        T2  m_age;
 
        void show_Person()
        {
                cout  << "name: " << this->m_name  << "age: " << this->m_age  << endl;
        }
};
 
 
void PrintPerson(Person<string,  int> &p)
{
        p.show_Person();
}
 
void test01()
{
        Person<string, int> p("孙悟空", 999);
        PrintPerson(p);
 
}
 
int main()
{
        test01();
        return 0;
}
<<
name: 孙悟空age:999

2.4.2参数模板化---将对象中的参数变为函数模板进行传递

#include <iostream>
using namespace std;
#include <string>
 
 
template<class  T1, class T2>
class Person
{
public:
        Person(T1 name, T2 age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        T1  m_name;
        T2  m_age;
 
        void show_Person()
        {
                cout  << "name: " << this->m_name  << "age: " << this->m_age  << endl;
        }
};
 
template<class  T1, class T2>
void PrintPerson(Person<T1, T2> &p)
{
        p.show_Person();
}
 
void test01()
{
        Person<string, int> p("八戒", 888);
        PrintPerson(p);
 
}
 
int main()
{
        test01();
        return 0;
}
<<
name: 八戒age:888

2.4.3整个类模板化---将这个对象类型模板化进行传递

#include <iostream>
using namespace std;
#include <string>
 
 
template<class  T1, class T2>
class Person
{
public:
        Person(T1 name, T2 age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
 
        T1  m_name;
        T2  m_age;
 
        void show_Person()
        {
                cout  << "name: " << this->m_name  << "age: " << this->m_age  << endl;
        }
};
 
template<class  T>
void PrintPerson(T &p)
{
        p.show_Person();
}
 
void test01()
{
        Person<string, int> p("沙和尚", 777);
        PrintPerson(p);
 
}
 
int main()
{
        test01();
        return 0;
}
<<
name: 沙和尚age:777

2.5类模板与继承

当类模板碰到继承

1)当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型

2)不指定会报错,因为编译器无法给子类分配内存

#include <iostream>
using namespace std;
 
 
template<class  T>
class Base
{
public:
        T  m;
};
 
class Son:  public Base<int>
{
public:
        Son()
        {
                cout  <<  "子类对象的创建" << endl;
        }
 
};
 
void test01()
{
        Son  c;
}
 
int main()
{
        test01();
        return 0;
}
<<子类对象的创建

3)想灵活指定父类中T的类型,子类也需要变为类模板

#include <iostream>
using namespace std;
 
 
template<class  T>
class Base
{
public:
        T  m;
};
 
class Son:  public Base<int>
{
public:
        Son()
        {
                cout  <<  "子类对象的创建" << endl;
        }
 
};
 
template<class  T1, class T2>
class Son2:  public Base<T2>
{
public:
        T1  m_name;
        Son2()
        {
                cout  <<  "子类对象灵活的创建" << endl;
        }
 
};
 
void test01()
{
        Son2<int,  char>S2;
}
 
int main()
{
        test01();
        return 0;
}
<<子类对象灵活的创建

2.6类模板成员函数类外实现

#include <iostream>
using namespace std;
#include <string>
 
template<class  T1, class T2>
class Person
{
public:
        T1  m_name;
        T2  m_age;
        Person(T1 name, T2 age);
        void Person_show();
        
};
 
 
template<class  T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
        this->m_name  = name;
        this->m_age  = age;
}
 
template<class  T1, class T2>
void Person<T1,  T2>::Person_show()
{
        cout  << "name: " << this->m_name  << "  age: " << this->m_age << endl;
}
 
void test01()
{
        Person<string,  int>P("huluwa", 23);
        P.Person_show();
}
 
int main()
{
        test01();
        return 0;
}
<<
name: huluwa  age: 23

2.7类模板的分文件编写

类模板中成员函数创建时机在调用阶段,会导致文件编写时链接不到

一般有两种方式来写类模板的分文件编写

1)直接包含.cpp源文件

2)将声明和实现写到同一个文件,并将后缀名改为.hpp。.hpp是约定的名称,不是强制名称

PS:一般用第二种方法

.cpp文件

#include <iostream>
using namespace std;
#include <string>
#include "Class_template_file_preparation.hpp"
void test01()
{
        Person<string,  int>P("huluwa", 23);
        P.Person_show();
}
 
int main()
{
        test01();
        return 0;
}

.hpp文件

#pragma once
 
template<class  T1, class T2>
class Person
{
public:
        T1  m_name;
        T2  m_age;
        Person(T1 name, T2 age);
        void Person_show();
        
};
 
 
template<class  T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
        this->m_name  = name;
        this->m_age  = age;
}
 
template<class  T1, class T2>
void Person<T1,  T2>::Person_show()
{
        cout  << "name: " << this->m_name  << "  age: " << this->m_age << endl;
}

2.8类模板与友元

2.8.1全局函数类内实现

#include <iostream>
using namespace std;
#include <string>
 
template<class  T1, class T2>
class Person
{
        friend void printPerson(Person<T1, T2> p)
        {
                cout  <<  "姓名: " << p.m_name << "年龄:" << p.m_age << endl;
        }
public:
        Person(T1 name, T2 age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
private:
        T1  m_name;
        T2  m_age;
};
 
void test01()
{
        Person<string,  int> p("Tom", 20);
        printPerson(p);
 
}
 
int main()
{
        test01();
        return 0;
}
<<姓名: Tom年龄:20

2.8.2全局函数类外实现

需要让编译器提前知道全局函数的存在

#include <iostream>
using namespace std;
#include <string>
 
template<class  T1, class T2>
class Person;
 
template<class  T1, class T2>
void printPerson(Person<T1, T2>  p)
{
        cout  <<  "类外实现---" << "姓名: " << p.m_name << "年龄:" << p.m_age << endl; 
}
 
template<class  T1, class T2>
class Person
{
        friend void printPerson<>(Person<T1, T2>  p);
        
public:
        Person(T1 name, T2 age)
        {
                this->m_name  = name;
                this->m_age  = age;
        }
private:
        T1  m_name;
        T2  m_age;
};
 
 
 
void test01()
{
        Person<string,  int> p("huluwa", 20);
        printPerson(p);
 
}
 
int main()
{
        test01();
        return 0;
}
<<类外实现---姓名: huluwa年龄:20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值