C++函数模板与类模板

简介: C++函数模板与类模板

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

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

模板的特点:

  • 不可以直接使用,只是一个框架
  • 模板的通用并不是万能的

函数模板语法

函数模板作用:

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

语法:template<typename T>

  • template:声明创建模板
  • typename:表明其后面的符号是一种数据类型,可以用class代替
  • T:通用的数据类型,名称可以替换,通常为大写字母
#include<iostream>
using namespace std;
//函数模板
template<typename  T>//告诉编译器,后面的代码中紧跟着的T不要报错,T是一个通用数据类型
void mySwap(T& a, T& b)
{
  T temp = a;
  a = b;
  b = temp;
}
int main()
{
  int a = 10;
  int b = 20;
  //两种方法使用函数模板
  //自动类型推导
  mySwap(a, b);
  cout << "a=" << a << "\tb=" << b << endl;
  //显式指定类型
  mySwap<int>(a, b);
  cout << "a=" << a << "\tb=" << b << endl;
  return 0;
}

函数模板注意事项

  • 自动类型推导,必须推导出一致的数据类型T才可以使用
  • 模板必须要确定出T的数据类型,才可以使用
//模板必须要确定出T的数据类型,才可以使用
template<typename  T>//注释掉可正常运行
void func(T& a, T& b)
{
  cout << "hello" << endl;
}
int main()
{
  func();//没有与参数列表匹配的函数模板实例
  return 0;
}

普通函数与函数模板区别

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换
  • 建议使用显示指定类型的方式调用函数模板,因为自己可以确定通用类型T
template<typename  T>
T func(T a, T b)
{
  return a + b;
}
int main()
{
  int a = 10;
  char b = 10;
    //如果利用显示指定类型的方式,可以发生隐式类型转换
  cout << func<int>(a, b) << endl;//可正常运行
  //如果利用自动类型推导,不会发生隐式类型转换
    cout << func(a, b) << endl;//会报错
  return 0;
}

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

  • 如果函数模板和普通函数都可以实现,优先调用普通函数
  • 可以通过空模板参数列表来强制调用函数模板
  • 函数模板也可以发生重载
  • 如果函数模板可以更好地匹配,优先调用函数模板
  • 既然提供了函数模板,最好就不要提供普通函数,否则容易出现二义性
void func(int a, int b)
{
  cout << "普通函数" << endl;
}
template<typename  T>
void func(T a, T b)
{
  cout << "模板" << endl;
}
int main()
{
  //如果函数模板和普通函数都可以实现,优先调用普通函数
  func(1, 1);
  //通过空模板参数列表来强制调用函数模板
  func<>(1, 1);
  return 0;
}
template<typename  T>
void func(T a, T b)
{
  cout << "模板" << endl;
}
template<typename  T>
void func(T a, T b,T c)
{
  cout << "重载的模板" << endl;
}
int main()
{
  func<>(1, 1);
  //函数模板也可以发生重载
  func<>(1, 1, 1);
  return 0;
}

模板的局限性

模板不是万能的,有些特定数据类型,需要用具体化方式做特殊实现

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板
class Person
{
public:
  Person(string name, int age)
  {
    m_name = name;
    m_age = age;
  }
  string m_name;
  int m_age;
};
template<typename  T>
bool func(T& a, T& b)
{
  if (a == b)
    return true;
  else
    return false;
}
//利用具体化Person的版本实现代码,具体化优先调用
template<> bool func(Person& a, Person& b)
{
  if (a.m_name == b.m_name && a.m_age == b.m_age)
    return true;
  else
    return false;
}

类模板语法

类模板作用:

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

语法:template<typename T>

  • template:声明创建模板
  • typename:表明其后面的符号是一种数据类型,可以用class代替
  • T:通用数据类型,名称可以替换,通常为大写字母

类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板

template<class NameType, class AgeType>
class Person
{
public:
  Person(NameType name, AgeType age)
  {
    this->m_name = name;
    this->m_age = age;
  }
  void show()
  {
    cout << m_name << m_age << endl;
  }
  NameType m_name;
  AgeType m_age;
};
int main()
{
  Person<string,int> a("张三", 10);
  a.show();
  return 0;
}

类模板与函数模板区别

类模板与函数模板区别主要有两点:

  • 类模板没有自动类型推导的使用方式
  • 类模板在模板参数列表中可以有默认参数
//类模板中可以有默认参数
template<class NameType, class AgeType = int>
class Person
{
public:
  Person(NameType name, AgeType age)
  {
    this->m_name = name;
    this->m_age = age;
  }
  void show()
  {
    cout << m_name << m_age << endl;
  }
  NameType m_name;
  AgeType m_age;
};
int main()
{
  //类模板没有自动类型推导的使用方式
  //Person a("张三", 10);
  Person<string> a("张三", 10);//只能用显示指定类型
  a.show();
  return 0;
}

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

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建
class A
{
public:
  void show()
  {
    cout << "A" << endl;
  }
};
template<typename T>
class B
{
public:
  T t;
  void show()
  {
    t.show();
  }
};
int main()
{
  B<A> b;
  b.show();
  return 0;
}

类模板对象做函数参数

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

  • 指定传入的类型:直接显示对象的数据类型
  • 参数模板化:将对象中的参数变为模板进行传递
  • 整个类模板化:将这个对象类型模板化进行传递
  • 使用比较广泛的是第一种:指定传入类型
template<typename T>
class A
{
public:
  A(T t)
  {
    this->m_t = t;
  }
  T m_t;
  void show()
  {
    cout << m_t << endl;
  }
};
//指定传入类型
void show(A<int>& a)
{
  a.show();
}
int main()
{
  A<int> a(10);
  show(a);
  return 0;
}
template<typename T>
class A
{
public:
  A(T t)
  {
    this->m_t = t;
  }
  T m_t;
  void show()
  {
    cout << m_t << endl;
  }
};
//参数模板化
template<typename T1>
void show(A<T1>& a)
{
  a.show();
}
int main()
{
  A<int> a(10);
  show(a);
  return 0;
}
template<typename T>
class A
{
public:
  A(T t)
  {
    this->m_t = t;
  }
  T m_t;
  void show()
  {
    cout << m_t << endl;
  }
};
//参数模板化
template<typename T>
void show(T a)
{
  a.show();
  cout << "T的类型为" << typeid(T).name() << endl;
}
int main()
{
  A<int> a(10);
  show(a);
  return 0;
}

类模板与继承

  • 当子类继承的父类是一个类模板时,子类在声明时,要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需变为类模板
template<typename T>
class A
{
};
//必须知道父类中T的类型,才能继承给子类
class B :public A<int>
{
};
class A
{
};
//如果想灵活指定父类中T的类型,子类也需要变为类模板
template<class T>
class B :public A<T>
{
};

类模板成员函数类外实现

template<typename T1, class T2>
class Person
{
public:
  Person(T1 name, T2 age);
  T1 m_name;
  T2 m_age;
};
//构造函数类外实现
template<class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
  this->m_age = age;
  this->m_name = name;
}
template<typename T1, class T2>
class Person
{
public:
  void show();
  T1 m_name;
  T2 m_age;
};
//成员函数类外实现
template<class T1, class T2>
void Person<T1, T2>::show()
{
  cout << m_name << "\t" << m_age << endl;
}

类模板分文件编写

问题:

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

两种解决方式:

  • 直接包含.cpp源文件
  • 将声明和实现写到同一个文件中,并更改后缀名为.hpp.hpp是约定的名称,不是强制规定

类模板与友元

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

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

建议全局函数做类内实现,用法简单,而且编译器可以直接识别

template<class T1>
class Test
{
  friend void func(Test<string> test)
  {
    cout << "全局函数类内实现 " << test.m_name << endl;
  }
  T1 m_name;
public:
  Test(T1 name)
  {
    this->m_name = name;
  }
};
int main()
{
  Test<string> a("张三");
  func(a);
  return 0;
}
template<class T1>
class Test;
template<class T1>
void func(Test<T1> test);
//用到的东西要提前让编译器知道
template<class T1>
class Test
{
  //加空模板参数列表,调用函数模板
  friend void func<>(Test<T1> test);
  T1 m_name;
public:
  Test(T1 name)
  {
    this->m_name = name;
  }
};
template<class T1>
void func(Test<T1> test)
{
  cout << "全局函数类外实现 " << test.m_name << endl;
}
int main()
{
  Test<string> a("张三");
  func(a);
  return 0;
}

自制数组类

直接包含.hpp即可使用

#pragma once
//自己的通用数组类
#include<iostream>
#include<string>
using namespace std;
template<class T>
class MyArray
{
public:
  MyArray(int capacity)
  {
    cout << "有参构造" << endl;
    this->m_Capacity = capacity;
    this->m_Size = 0;
    this->pAddress = new T[this->m_Capacity];
  }
  MyArray(const MyArray& arr)
  {
    cout << "拷贝构造" << endl;
    this->m_Capacity = arr.m_Capacity;
    this->m_Size = arr.m_Size;
    //深拷贝
    this->pAddress = new T[arr.m_Capacity];
    //将arr中的数据都拷贝过来
    for (int i = 0; i < this->m_Size; i++)
    {
      this->pAddress[i] = arr.pAddress[i];
    }
  }
  ~MyArray()
  {
    if (this->pAddress != NULL)
    {
      cout << "析构" << endl;
      delete[] this->pAddress;
      this->pAddress = NULL;
    }
  }
  MyArray& operator=(const MyArray& arr)
  {
    //先判断原来堆区是否有数据,如果有,先释放
    if (pAddress != NULL)
    {
      cout << "operator=调用" << endl;
      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[i];
    }
    return *this;
  }
  //尾插法
  void Push_Back(const T& val)
  {
    if (this->m_Capacity == this->m_Size)
    {
      return;
    }
    this->pAddress[this->m_Size] = val;
    this->m_Size++;
  }
  //尾删法
  void Pop_Back()
  {
    //让用户访问不到最后一个元素,即是尾删
    if (m_Size == 0)
    {
      return;
    }
    this->m_Size--;
  }
  //通过下标访问数组中的元素
  T& operator[](int index)
  {
    return this->pAddress[index];//不考虑边界,由用户自行处理
  }
  //返回数组容量
  int getCapacity()
  {
    return this->m_Capacity;
  }
  //返回数组大小
  int getSize()
  {
    return this->m_Size;
  }
private:
  T* pAddress;//指针指向堆区开辟的真实数组
  int m_Capacity;
  int m_Size;
};
目录
相关文章
|
20天前
|
编译器 C++
【C++】——初识模板
【C++】——初识模板
30 1
【C++】——初识模板
|
13天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
13天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
13天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
20天前
|
编译器 C++ 容器
【C++】String常见函数用法
【C++】String常见函数用法
13 1
|
26天前
|
存储 算法 编译器
c++--类(上)
c++--类(上)
|
13天前
|
存储 C++
C++(五)String 字符串类
本文档详细介绍了C++中的`string`类,包括定义、初始化、字符串比较及数值与字符串之间的转换方法。`string`类简化了字符串处理,提供了丰富的功能如字符串查找、比较、拼接和替换等。文档通过示例代码展示了如何使用这些功能,并介绍了如何将数值转换为字符串以及反之亦然的方法。此外,还展示了如何使用`string`数组存储和遍历多个字符串。
|
22天前
|
存储 C++
C++ dll 传 string 类 问题
C++ dll 传 string 类 问题
16 0
|
27天前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
30 0
|
27天前
|
并行计算 测试技术 开发工具
【简历模板】c/c++软件工程师
【简历模板】c/c++软件工程师
39 0