量化交易之C++篇 - 函数模板的基本用法

// 函数模板的基本用法
#include <iostream>

using namespace std;

void swapInt(int& a, int& b) {

	int temp = a;
	a = b;
	b = temp;
}

void test01() {

	int a = 1;
	int b = 2;

	cout << "a: " << a << "     b: " << b << endl;
	swapInt(a, b);
	cout << "a: " << a << "     b: " << b << endl;
}

void swapDouble(double& a, double& b) {

	double temp = a;
	a = b;
	b = temp;
}

void test02() {

	double a = 1;
	double b = 2;

	cout << "a: " << a << "     b: " << b << endl;
	swapDouble(a, b);
	cout << "a: " << a << "     b: " << b << endl;

}

// 类型参数化 泛型编程 - 模板技术
template<class T> // 告诉编译器 下面如果出现T 不要报错, T是一个通用的类型
void mySwap(T& a, T& b) {
	T temp = a;
	a = b;
	b = temp;
}

void test03() {

	int a = 10;
	int b = 20;

	double c = 30;
	double d = 40;

	char e = 'e';
	char f = 'f';

	cout << "a: " << a << "     b: " << b << endl;
	mySwap(a, b); // 自动类型推导
	cout << "a: " << a << "     b: " << b << endl;

	cout << "c: " << c << "     d: " << d << endl;
	mySwap(c, d);
	cout << "c: " << c << "     d: " << d << endl;

	cout << "e: " << e << "     f: " << f << endl;
	mySwap(e, f);
	cout << "e: " << e << "     f: " << f << endl;
    
	cout << "a: " << a << "     b: " << b << endl;
	mySwap<int>(a, b); // 显示指定类型
	cout << "a: " << a << "     b: " << b << endl;
}

int main() {

	//test01();

	//test02();

	test03();

	return EXIT_SUCCESS;
}
// 利用模板实现char 和 int 的数组排序
#include <iostream>

using namespace std;

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 charArray[] = "helloworld";
	int len = strlen(charArray);

	mySort(charArray, len);

	printArray(charArray, len);
}

int main() {

	test01();

	return EXIT_SUCCESS;
}
// 模板的局限性以及解决
#include <iostream>
#include <string>

using namespace std;

class Person {

public:
	Person(string name, int age) {
		this->name = name;
		this->age = age;
	}

	string name;
	int age;
};

template<class T>
bool myCompare(T &a, T &b) {
	return (a == b);
}

void test01() {
	int a = 10;
	int b = 20;

	int result = myCompare(a, b);

	cout << "result = " << result << endl;
}

// 具体化自定义数据类型
template<> bool myCompare<Person>(Person& personA, Person& personB) {
	return (personA.age == personB.age);
}

void test02() {
	Person person1("roger", 19);
	Person person2("reiri", 19);

	int resultPerson = myCompare(person1, person2);
	cout << "resultPerson:" << resultPerson << endl;
}

int main() {

	//test01();

	test02();

	return EXIT_SUCCESS;
}
// 类模板的基本使用
#include <iostream>
#include <string>

using namespace std;

template<class NameType, class AgeType>
class Person {

public:
	Person(NameType name, AgeType age) {
		this->name = name;
		this->age = age;
	}

	void showInfomation() {
		cout << "name: " << this->name << "  age: " << this->age << endl;
	}

	NameType name;
	AgeType age;
};

void test01() {

	Person<string, int> person("kurohige", 45);

	person.showInfomation();
}

// 1. 指定传入类型
void doWork2(Person<string, int>& person) {

	person.showInfomation();
}

void test02() {

	Person<string, int> person("akagami", 40);

	doWork2(person);
}

// 2. 参数模板化
template<class T1, class T2>
void doWork3(Person<T1, T2>& person) {

	// 查看类型
	cout << "type T1: " << typeid(T1).name() << endl;
	cout << "type T2: " << typeid(T2).name() << endl;

	person.showInfomation();
}

void test03() {
	Person<string, int> person("big mom", 55);

	doWork3(person);
}

// 3. 整体模板化
template<class PersonType>
void doWork04(PersonType& person) {

	cout << "PersonType: " << typeid(PersonType).name() << endl;

	person.showInfomation();
}


void test04() {
	Person<string, int> person("gai do", 56);
	doWork04(person);
}

int main() {

	//test01();

	//test02();

	//test03();

	test04();

	return EXIT_SUCCESS;
}
// 类模板的继承问题
#include <iostream>
#include <string>

using namespace std;

template<class T> 
class Base {

public:
	T a;

};

// child 继承与 base 必须告诉base中的T的类型, 否则T无法分配内存
class Child : public Base<int> {

};

template<class T1, class T2>
class Child2 : public Base<T2> {

public:

	Child2() {
		cout << typeid(T1).name() << endl;
		cout << typeid(T2).name() << endl;
	}

	T1 b;
};

void test01() {
	Child2<int, double> child;
}

int main() {

	test01();

	return EXIT_SUCCESS;
}
// 类模板类外实现成员函数
#include <iostream>
#include <string>

using namespace std;

template<class T1, class T2>
class Person {

public:
	Person(T1 name, T2 age);

	void showInfomation();

	T1 name;
	T2 age;
};

template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {

	this->name = name;
	this->age = age;
}

template <class T1, class T2>
void Person<T1, T2>::showInfomation() {

	cout << "name: " << this->name << "   age: " << this->age;
}

int main() {

	Person<string, int> person("roger", 19);

	person.showInfomation();

	return EXIT_SUCCESS;
}
// Person.hpp
#pragma once

#include <iostream>
#include <string>

using namespace std;

template<class T1, class T2>
class Person {

public:
	Person(T1 name, T2 age);

	void showInfomation();

	T1 name;
	T2 age;
};

template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {

	this->name = name;
	this->age = age;
}

template <class T1, class T2>
void Person<T1, T2>::showInfomation() {

	cout << "name: " << this->name << "   age: " << this->age;
}
// 类模板与友元函数 - 类外实现
#include <iostream>
#include <string>

#include "Person.hpp" // 模板不要做份文件编写, 写入一个类中即可, 类内进行声明和实现, 最后把后缀名改为 .hpp 约定俗成

using namespace std;

void test01() {
	Person<string, int> person("roger", 19);

	person.showInfomation();
}

int main() {

	test01();

	return EXIT_SUCCESS;
}
// 类模板与友元函数 - 类内实现
#include <iostream>

using namespace std;

template<class T1, class T2>
class Person {
	// 友元函数类内实现
	friend void printPersonInside(Person<T1, T2>& person) {
		cout << "T1: " << person.name << "  T2: " << person.age << endl;
	}

	template<class T1, class T2>
	friend void printPersonOutSide(Person<T1, T2>& person); // 普通函数

public:

	Person(T1 name, T2 age) {
		this->name = name;
		this->age = age;
	}

private:
	T1 name;
	T2 age;

};

// 类外实现 类模板的友元函数
template<class T1, class T2>
void printPersonOutSide(Person<T1, T2>& person) {
	cout << "outside T1: " << person.name << "  inside T2: " << person.age << endl;
}

void test01() {

	Person<string, int> person("maruko", 26);
	printPersonInside(person);
}

void test02() {
	Person<string, int> person("maruko", 26);
	printPersonOutSide(person);
}

int main() {

	//test01();

	test02();

	return EXIT_SUCCESS;
}
// MyArray.hpp
#pragma once

#include <iostream>

using namespace std;

template<class T>
class MyArray {

public:
	explicit MyArray(int capacity) { // 防止隐式类型转换: 防止 MyArray array = 10; 写法
		this->capacity = capacity;
		this->size = 0;
		this->pAddress = new T[this->capacity];
	}

	MyArray(const MyArray& array) {
		this->capacity = array.capacity;
		this->size = array.size;
		this->pAddress = new T[this->capacity];

		for (int i = 0; i < this->size; i++) {
			this->pAddress[i] = array[i];
		}
	}

	// 赋值操作符= 重载
	MyArray& operator=(const MyArray& array) {
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
		}

		this->capacity = array.capacity;
		this->size = array.size;
		this->pAddress = new T[this->size];
		for (int i = 0; i < this->size; i++) {
			this->pAddress[i] = array[i];
		}
	}

	T& operator[](int index) {
		return this->pAddress[index];
	}

	int getSIze() {
		return this->size;
	}

	int getCapacity() {
		return this->capacity;
	}

	void pushBack(T value) {
		this->pAddress[this->size] = value;
		this->size++;
	}

	void printArray() {
		for (int i = 0; i < this->size; i++) {
			cout << "array[" << i << "]: " << this->pAddress[i] << endl;
		}
	}


	~MyArray() {
		if (this->pAddress != NULL) {
			delete[] this->pAddress;
			this->pAddress = NULL;
		}
	}

private:
	T* pAddress; // 指向堆区内存
	int capacity;
	int size;
};
// 类模板的应用 - 数组类封装
#include <iostream>
#include "MyArray.hpp"

using namespace std;

void test01() {

	MyArray<int> array(10);
	for (int i = 0; i < 10; i++) {
		array.pushBack(i + 1);
	}

	array.printArray();
}

int main() {

	test01();

	return EXIT_SUCCESS;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值