自学内容网 自学内容网

C++:模板编程入门

什么是模板编程

假设我们需要实现对整形、浮点型、双精度型数据的交换函数,一般情况我们需要重载三个函数,但是使用模板,我们使用一个函数就能解决。
函数在编译时进行重载。

#include <iostream>

using namespace std;

template <typename T>
void swapfun(T& a, T& b) {
  T t;
  t = a;
  a = b;
  b = t;
}

void test0708(void) {
  float a = 9.9;
  float b = 5.6;
  cout << "a:" << a << endl;
  cout << "b:" << b << endl;
  cout << "swapfun:" << endl;
  swapfun(a, b);
  cout << "a:" << a << endl;
  cout << "b:" << b << endl;

  int i = 90;
  int j = 11;
  cout << "i:" << i << endl;
  cout << "j:" << j << endl;
  cout << "swapfun:" << endl;
  swapfun(i, j);
  cout << "i:" << i << endl;
  cout << "j:" << j << endl;
}

在这里插入图片描述

模板的理解和几个细节

模板功能
1、编写类型无关函数
2、是一种抽象的维度
3、使用模板写库函数更好,按需生产
4、拿复杂的换劳动量

可以定义多个模板名称



template <typename T ,typename T1>
void swapfun(T& a, T1& b) {
  cout << "a:" << a << endl;
  cout << "b:" << b << endl;
}

void test0708(void) {
  float a = 9.9;
  int b = 5.6;

  swapfun(a, b);
}

在这里插入图片描述

函数模板和类模板

函数模板:在函数参数列表当中使用
类模板:类成员变量、类成员函数的参数列表

多模板参数的类模板

template <typename T>
class People {
 private:
  /* data */
  int age;

 public:
  People(/* args */);
  People(int a);

  void myprint(T x);
};
template <typename T>
void People<T>::myprint(T x) {
  cout << "x = " << x << endl;
  cout << "age = " << age << endl;
}
/// @brief /

template <typename T>
People<T>::People(int a) {
  age = a;
}

void test070802(void) {
  int a = 90;
  double b = 89.99;

  People<int> p1(12);
  p1.myprint(a);

  People<double> p2(12);
  p2.myprint(b);
}

同类型多参数

template <typename T>
class People {
 private:
  /* data */
  int age;

 public:
  People(/* args */);
  People(int a);

  void myprint(T x);
  void funtest(T a, T b);
};

template <typename T>
void People<T>::funtest(T a, T b) {
  cout << "a = " << a << endl;
  cout << "b = " << b << endl;
}
template <typename T>
void People<T>::myprint(T x) {
  cout << "x = " << x << endl;
  cout << "age = " << age << endl;
}
/// @brief /

template <typename T>
People<T>::People(int a) {
  age = a;
}

void test070802(void) {
  int a = 90;
  double b = 89.99;

  People<int> p1(12);
  p1.funtest(a,2);

  People<double> p2(12);
  p2.funtest(b,88.88);
}

不同类型多参数

template <typename T,typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People(/* args */);
  People(int a);

  void myprint(T x);
  void funtest(T a, T1 b);
};

template <typename T,typename T1>
void People<T,T1>::funtest(T a, T1 b) {
  cout << "a = " << a << endl;
  cout << "b = " << b << endl;
}
template <typename T,typename T1>
void People<T,T1>::myprint(T x) {
  cout << "x = " << x << endl;
  cout << "age = " << age << endl;
}
/// @brief /

template <typename T,typename T1>
People<T,T1>::People(int a) {
  age = a;
}

void test070802(void) {
  int a = 90;
  double b = 89.99;

  People<int,int> p1(12);
  p1.funtest(a,2);

  People<double,float> p2(12);
  p2.funtest(b,88.88);
}

成员函数在类内部的模板类

template <typename T, typename T1>
class People {
 private:
  /* data */
  int age;

 public:
  People(/* args */);
  People(int a);

  void myprint(T x);
  void funtest(T a, T1 b) {
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
  }
};

// template <typename T,typename T1>
// void People<T,T1>::funtest(T a, T1 b) {
//   cout << "a = " << a << endl;
//   cout << "b = " << b << endl;
// }
template <typename T, typename T1>
void People<T, T1>::myprint(T x) {
  cout << "x = " << x << endl;
  cout << "age = " << age << endl;
}
/// @brief /

template <typename T, typename T1>
People<T, T1>::People(int a) {
  age = a;
}

void test070802(void) {
  int a = 90;
  double b = 89.99;

  People<int, int> p1(12);
  p1.funtest(a, 2);

  People<double, float> p2(12);
  p2.funtest(b, 88.88);
}

学习记录,侵权联系删除。
来源:朱老师物联网大课堂


原文地址:https://blog.csdn.net/qq_43441284/article/details/140422568

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!