8.6 C++ 泛型化编程态

C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。

函数模板的基本使用: 函数模板就是要实现类型参数化,实现泛型编程,就是可以动态的调整数据类型.

#include <iostream>
#include <typeinfo>

using namespace std;

// template<> 告诉编译器,下面如果出现T不要报错,T是一个通用的类型
template<class T>
void MySwap(T &x, T &y)
{
  T tmp = x;
  x = y;
  y = tmp;
}

template<class T>
T MyAdd(T &x, T &y)
{
  return x + y;
}

// 此处的typename = class
template<typename T>
void MyPrint()
{
  T number;
  cout << "type = " << typeid(T).name() << endl;
}

int main(int argc, char *argv[])
{
  // 自动类型推导: 必须要有参数才能推到(根据参数来确定类型)
  int x = 10, y = 20;
  MySwap(x, y);
  cout << "x= " << x << endl;

  int ret = MyAdd(x, y);
  cout << "x+y = " << ret << endl;

  // 手动类型指定: 如果参数不一致,可能会报错,此时我们需要告诉它类型
  MySwap<int>(x, y);
  cout << "x= " << x << endl;

  // 针对无参数函数处理: 有些函数没有参数,我们需要指定模板默认类型
  MyPrint<int>();
  MyPrint<double>();

  system("pause");
  return 0;
}

我们在上面的案例基础上进行一定的加强,通过模板实现一个选择排序,我们可以传入任意的数据类型,都可被解析.

#include <iostream>
#include <typeinfo>

using namespace std;

template<class T>
void MySwap(T &x, T &y)
{
  T tmp = x;
  x = y;
  y = tmp;
}

template<class T>
void SelectSort(T Array[], int len)
{
  for (int x = 0; x < len; x++)
  {
    int max = x;
    for (int y = x + 1; y < len; y++)
    {
      if (Array[max] > Array[y])
        max = y;
    }
    if (max != x)
      MySwap(Array[max], Array[x]);
  }
}

template<class T>
void MyPrint(T Array[], int len)
{
  for (int x = 0; x < len; x++)
    cout << Array[x] << " ";
}

int main(int argc,char *argv[])
{
  int Int_Array[10] = { 4, 7, 8, 2, 1, 8, 0, 3, 2, 7 };
  SelectSort<int>(Int_Array, 10);
  MyPrint<int>(Int_Array, 10);

  char Char_Array[] = "hello lyshark";
  int len = sizeof(Char_Array) / sizeof (char);
  SelectSort<char>(Char_Array, len);
  MyPrint<char>(Char_Array, len);

  system("pause");
  return 0;
}

实现模板具体化: 通过自定义模板函数,解决模板的局限性问题.

#include <iostream>

using namespace std;

class Student
{
public:
  char *m_name;
  int m_age;
public:
  Student(char *name, int age)
  {
    this->m_name = name;
    this->m_age = age;
  }
};

template<class Student>
bool MyCompare(Student &x, Student &y)
{
  if (x.m_age == y.m_age)
    return true;
  return false;
}


int main(int argc, char *argv[])
{
  Student stu1("lyshark", 22);
  Student stu2("admin", 33);

  bool ret = MyCompare(stu1, stu1);
  cout << ret << endl;

  bool ret1 = MyCompare(stu1, stu2);
  cout << ret1 << endl;

  system("pause");
  return 0;
}

定义并使用类模板: 类模板不支持类型的自动推导,所以必须在调用时Student<string, int>显式指定好类型.

#include <iostream>
#include <string>

using namespace std;

template<class NameType = string,class AgeType = int>  // 类模板可以指定默认参数
class Student
{
public:
  string m_name;
  int m_age;

public:
  Student(NameType name,AgeType age)
  {
    this->m_name = name;
    this->m_age = age;
  }
  void show() { cout << "name = " << m_name << endl; }
};

template<class Student>
bool MyCompare(Student &x, Student &y)
{
  if (x.m_age == y.m_age)
    return true;
  return false;
}

int main(int argc, char *argv[])
{
  // 调用类模板是要在类后面添加参数列表
  Student<string, int> stu1("lyshark", 25);
  stu1.show();

  system("pause");
  return 0;
}

类模板做函数参数传递: 此处我们将类模板Student<string, int>当做函数参数传递给MyPrint函数.

#include <iostream>
#include <string>

using namespace std;

template<class NameType,class AgeType>
class Student
{
public:
  string m_name;
  int m_age;

public:
  Student(NameType name,AgeType age)
  {
    this->m_name = name;
    this->m_age = age;
  }
  void show() { cout << "name = " << m_name << endl; }
};

void MyPrintA(Student<string, int> &ptr)
{ ptr.show(); }

template<class T1,class T2>
void MyPrintB(Student<T1,T2> &ptr)
{ ptr.show(); }

template<class T>
void MyPrintC(T &ptr)
{ ptr.show(); }

int main(int argc, char *argv[])
{
  // 1. 指定传入的类型直接调用
  Student<string, int> stu1("lyshark", 25);
  MyPrintA(stu1);

  // 2. 参数模板化调用
  Student<string, int> stu2("admin", 10);
  MyPrintB(stu2);

  // 3.整体模板化调用
  Student<string, int> stu3("root", 10);
  MyPrintC(stu3);

  system("pause");
  return 0;
}

类模板类内定义类外实现: 类模板同样支持类内定义模板类型,在类外部对其进行具体的实现.


#include <iostream>
#include <string>

using namespace std;

template<class NameType,class AgeType>
class Student
{
public:
  string m_name;
  int m_age;

public:
  Student(NameType name, AgeType age);
  void show();
};

// 类外实现成员构造函数
template <class NameType,class AgeType>
Student<NameType, AgeType>::Student(NameType name, AgeType age)
{
  this->m_name = name;
  this->m_age = age;
}

// 类外实现打印函数
template <class NameType,class AgeType>
void Student<NameType, AgeType>::show()
{
  cout << "Name = " << this->m_name << endl;
}

int main(int argc, char *argv[])
{
  Student<string, int> stu("lyshark", 20);
  stu.show();

  system("pause");
  return 0;
}

类模板友元函数类内实现: 友元函数就是可以让类外直接访问的函数,调用类内友元函数就像调用全局函数一样.

#include <iostream>
#include <string>

using namespace std;

template<class NameType,class AgeType>
class Student
{
  // 友元函数的类内实现
  friend void show(Student<NameType, AgeType> &ptr)
  {
    cout << "name = " << ptr.m_name << endl;
  }

private:
  string m_name;
  int m_age;
public:
  Student(NameType name, AgeType age)
  {
    this->m_name = name;
    this->m_age = age;
  }
};


int main(int argc, char *argv[])
{
  Student<string, int> stu("lyshark", 20);
  // 此处调用,类似于全局调用
  show(stu);

  system("pause");
  return 0;
}

类模板友元函数类外实现: 类外实现同理,就是现在类内声明类型作为占位符,然后在类外进行实现.

#include <iostream>
#include <string>

using namespace std;

// 类外实现必须提前声明模板的存在
template<class T1, class T2> class Student;
template<class T1, class T2> void show(Student<T1, T2> & p);

template<class T1,class T2>
class Student
{
  // 友元函数类内实现,利用空参数列表声明 (占位符)
  friend void show<>(Student<T1, T2> &ptr);

private:
  string m_name;
  int m_age;

public:
  Student(T1 name, T2 age)
  {
    this->m_name = name;
    this->m_age = age;
  }
};

// 对友元函数的类外实现
template<class T1,class T2>
void show(Student<T1,T2> &ptr)
{
  cout << "name = " << ptr.m_name << endl;
}

int main(int argc, char *argv[])
{
  Student<string, int> stu("lyshark", 20);
  // 此处调用,类似于全局调用
  show(stu);

  system("pause");
  return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值