c++学习小结(模板11.6)

目录

函数模板语法

函数模板的作用:

函数模板注意事项

函数模板案例

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

模板的局限性

类模板的作用:

类模板与函数模板的区别

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

 类模板对象做函数参数

类模板与继承

类模板成员函数类外实现

类模板分文件编写

类模板与友元

全局函数类外实现-需要提前让编译器知道全局函数的存在

类模板案例


c++另一种编程思想称为泛型编程,主要利用的技术就是模板

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

函数模板语法

函数模板的作用:

建立一个通用函数我,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表

语法

template<typename T>

函数声明或定义

 解释:

template -- 声明创建模板

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

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

template<typename T> //typename可以替换为class
void mySwap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a = 10;
    int b = 20;

    //两种方式使用函数模板
        //1.自动类型推导
    mySwap(a, b);
        //2.显示指定类型
    mySwap<int>(a, b);
    cout << a << endl;   //20   
    cout << b << endl;   //10
    return 0;
}

函数模板注意事项

注意事项:

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

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

函数模板案例

案例描述:

利用函数模板封装一个排序的函数,可以对不同数据类型数组进行排序

排序规则从大到小,排序算法为选择排序

分别用char数组和int数组进行调试

template<class T>
void mySwap(T& a, T& b)
{
    T temp = a;
    a = b;
    b = temp;
}


template<class T>
void mySort(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        int max = i;
        for (int j = i + 1; j < len; j++)
        {
            if (arr[max] < arr[j])
            {
                max = j;
            }
        }
        if (max != i)
        {
            mySwap(arr[max], arr[i]);
        }
    }
}

template<class T>
void printArray(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << " ";

    }
    cout << endl;
}

void test01()
{
    char charArr[] = { "badcfe" };
    int len = sizeof(charArr) / sizeof(charArr[0]);
    mySort(charArr, len);
    printArray(charArr, len); //f e d c b f
}
void test02()
{
    int Intarr[] = { 4,5,7,2,1,9,8,3 };
    int len = sizeof(Intarr) / sizeof(int);
    mySort(Intarr, len);
    printArray(Intarr, len); // 9 8 7 6 5 4 3 2 1
}

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

        普通函数调用时可以发生自动类型转换(隐式类型转换)

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

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

普通函数和函数模板的调用规则

调用规则如下:

        如果函数模板和普通函数都可以实现,编译器优先调用普通函数

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

        函数模板也可以发生重载

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

模板的局限性

局限性:模板的通用性不是万能的(例如:传入一个自定义的类)

有些特定的数据类型,需要用具体化方式做特殊实现

利用具体化的模板,可以解决自定义类型的通用化

学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

类模板

类模板的作用:

        建立一个通用类,类中的成员 数据类型可以不具体指定,用一个虚拟的类型来代表

语法:

template<typename T>

解释:

template -- 声明创造模板

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

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

template<class NameType, class AgeType>
class Person
{
public:
    Person(NameType name, AgeType age)
    {
        this->m_Name = name;
        this->m_age = age;
    }


    NameType m_Name;
    AgeType m_age;
};

类模板与函数模板的区别

主要区别有两点:

        类模板没有自动类型推导的使用方式

        类模板在模板参数列表中有可以有默认参数

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

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

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

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

        类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成

 类模板对象做函数参数

学习目标:

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

一共有三种传入方式:

1.指定传入的类型 -- 直接显示对象的数据类型  经常使用

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

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

template<class T1,class T2>
class Person
{
public:
    Person(T1 name, T2 age)
    {
        this->m_name = name;
        this->m_age = age;
    }

    void showPerson()
    {
        cout << this->m_name << endl;
        cout << this->m_age << endl;
    }
    T1 m_name;
    T2 m_age;
};

//指定传入类型  *最常用*
void printPerson1(Person<string, int>&p)
{
    p.showPerson();
}
void test()
{
    Person<string, int>p("孙悟空",100);
    printPerson1(p);
}

//参数模板化
template<class T1,class T2>
void printPerson2(Person<T1,T2>&p)
{
    p.showPerson();
}
void test02()
{
    Person<string, int>p("孙悟空2", 200);
    printPerson2(p);
}

//整个类模板化
template<class T>
void printPerson3(T &p)
{
    p.showPerson();
}
void test03()
{
    Person<string, int>p("孙悟空3", 300);
    printPerson3(p);
}

类模板与继承

注意:

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

        如果不指定,编译器无法给子类分配内存

        如果想灵活指定出父类中T的类型,子类也需变为类模板

template<class T>
class Base
{
    T m;

};
class Son:public Base  //报错 必须要知道T的数据类型才能继承
{

};
template<class T>
class Base
{
    T m;

};
class Son:public Base<int> //正确
{

};

//如果想灵活指定父类中T类型,子类也需要变类模板
template<class T1,class T2>
class Son2 : public Base<T2>
{
    T1 obj;
};

void test()
{
    Son2<int, char>S2;
}

 

类模板成员函数类外实现

//类模板成员函数类外实现
template<class T1,class T2>
class Person
{
public:
    Person(T1 name, T2 age);
  /*  {
        this->m_name = name;
        this->m_age = age;
    }*/

    void showPerson();
    /*{
        cout << this->m_name << endl;
        cout <<this->m_age << endl;
    }*/

    T1 m_name;
    T2 m_age;
};

//构造函数类外实现
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>::showPerson()
{
    cout << this->m_name << endl;
    cout << this->m_age << endl;
}

void test()
{
    Person<string, int>p("Tom", 20);
    p.showPerson();
}

类模板分文件编写

问题:

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

 解决:

        1.直接包含.cpp源文件

        当类分为.h 和 .cpp时 包含cpp而不是h

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

         将h和cpp写到一起,将后缀名改为hpp

类模板与友元

全局函数类内实现-直接在类内声明友元即可

全局函数类外实现-需要提前让编译器知道全局函数的存在

template<class T1,class T2>
class Person;
//类外实现
template<class T1, class T2>
void printPerson2(Person<T1, T2> p)
{
    cout << p.m_name << endl;
    cout << p.m_age << endl;
}

template<class T1,class T2>
class Person
{
    //全局函数 类内实现
    friend void printPerson(Person<T1,T2> p)
    {
        cout << p.m_name << endl;
        cout << p.m_age << endl;
    }
    
    //全局函数 类外实现
    //加空模板参数列表
    //如果全局函数时类外实现,需要让编译器提前知道这个函数的存在(就是再类的声明之前先写函数)
    friend void printPerson2<>(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 test()
{
    Person<string, int>p("tom", 10);
    printPerson(p);
}

void test2()
{
    Person<string, int>p2("jack", 10);
    printPerson2(p2);
}

类模板案例

        可以对内置数据类型以及自定义数据类型的数据进行存储

        将数组中的数据存储到堆区

        构造函数中可以传入数组的容量

        提供对应的拷贝构造函数以及operator=防止浅拷贝问题

        提供尾插法和尾删法对数组中的数据进行增加和删除

        可以通过下标的方式访问数组中的元素

        可以获取数组中当前元素个数和数组的容量

MyArray.hpp

template<class T>
class MyArray
{
public:

	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	} 

	//拷贝构造
	MyArray(const MyArray & arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;  错 会浅拷贝

		//深拷贝
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}

	}

	//operator= 防止浅拷贝
	MyArray& operator=(const MyArray& arr)
	{
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress;
		}
		return *this;
	}

	//尾插法
	void Push_Badk(const T &val)
	{
		//判断容量够不够
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		else
		{
			this->pAddress[this->m_Size] = val;
			this->m_Size++;//更新数组大小
		}
	}

	//尾删法
	void Pop_Back()
	{
		if (this->m_Size== 0)
		{
			return;
		}
		else
		{
			this->m_Size--;
		}
	}

	//通过下标方式访问数组中的元素并且可以引用  arr[] = 100
	T& operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress;//指针指向堆区开辟的真实数组
	
	int m_Capacity;//数组容量

	int m_Size;//数组大小

};

main.cpp

template<class T>
class MyArray
{
public:

	//有参构造
	MyArray(int capacity)
	{
		this->m_Capacity = capacity;
		this->m_Size = 0;
		this->pAddress = new T[this->m_Capacity];
	} 

	//拷贝构造
	MyArray(const MyArray & arr)
	{
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		//this->pAddress = arr.pAddress;  错 会浅拷贝

		//深拷贝
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress[i];
		}

	}

	//operator= 防止浅拷贝
	MyArray& operator=(const MyArray& arr)
	{
		//先判断原来堆区是否有数据,如果有先释放
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
			this->m_Capacity = 0;
			this->m_Size = 0;
		}

		//深拷贝
		this->m_Capacity = arr.m_Capacity;
		this->m_Size = arr.m_Size;
		this->pAddress = new T[arr.m_Capacity];
		for (int i = 0; i < this->m_Size; i++)
		{
			this->pAddress[i] = arr.pAddress;
		}
		return *this;
	}

	//尾插法
	void Push_Badk(const T &val)
	{
		//判断容量够不够
		if (this->m_Capacity == this->m_Size)
		{
			return;
		}
		else
		{
			this->pAddress[this->m_Size] = val;
			this->m_Size++;//更新数组大小
		}
	}

	//尾删法
	void Pop_Back()
	{
		if (this->m_Size== 0)
		{
			return;
		}
		else
		{
			this->m_Size--;
		}
	}

	//通过下标方式访问数组中的元素并且可以引用  arr[] = 100
	T& operator[](int index)
	{
		return this->pAddress[index];
	}

	//返回数组容量
	int getCapacity()
	{
		return this->m_Capacity;
	}

	//返回数组大小
	int getSize()
	{
		return this->m_Size;
	}
	~MyArray()
	{
		if (this->pAddress != NULL)
		{
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress;//指针指向堆区开辟的真实数组
	
	int m_Capacity;//数组容量

	int m_Size;//数组大小

};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值