泛型與模版的應用

泛型編程即以一種獨立於任何特定類型的方式編寫代碼。

模板類型

一,模板參數

指定形式參數類型和返回參數類型, 模板參數不允許自動類型轉換

template<typename>

inline RT max(const T1& a, const T2& b)

{

return a > b? a:b;

}

int main()

{

int a, b;

a = 1;

b = 2;

max(a,b);

}

二,模板函數

#include <iostream>

#include <string>

//typename 意義時說明,T 是一個數據類型,不是一個類或者成員變量, 或者成員函數。並非強制使用typename, class同樣可以替換wq

template <typename>

inline T func(T& t) {

return t;

}

using namespace std;

int main() {

string hello = "hello";

//編譯器猜測類型

cout << func(hello) << endl;

//指明類型

cout << func<string>(hello) << endl;/<string>

return 0;

}

1.隱式指定

template <typedef>

const T& max(const T& a, const T& n)

{

retun a > b ? a :b;

}

int main()

{

int a, b;

a = 1;

b = 4;

//隱式轉換

max(a, b);

return 0;

}

2.顯式轉換

template <typedef>

const T& max(const T& a, const T& n)

{

retun a > b ? a :b;

}

int main()

{

int a, b;

a = 1;

b = 4;

//顯式轉換

max(a, b);

return 0;

}

3.數據類型強制轉換

template <typedef>

const T& max(const T& a, const T& n)

{

retun a > b ? a :b;

}

int main()

{

int a;

double b;

a = 1;

b = 4.0;

//隱式轉換

max<double>(static_cast<double>a, b);/<double>/<double>

return 0;

}

三, 模板重載函數

inline const int& max(const int& a, const int& b)

{

return a > b ? a : b;

}

template<typename>

inline const T& max(const T& a, const T& b)

{

return a > b? a: b;

}

template<typename>

inline const T& max(const T& a, const T& b, const T& c)

{

return max(max(a, b), c);

}

int main()

{

max(1, 7); //調用int 類型的重載函數

max(1, 2, 3); //調用三個參數䣌模板

return 0;

}

三,類模板

#include <iostream>

#include <string>

using namespace std;

template <class>

class Hello {

public:

void say(const T& t) {

cout << t << endl;

}

};

int main() {

Hello<string>* hello = new Hello<string>();/<string>/<string>

hello->say("hello");

return 0;

}

模版定義和實現分離時

//test.h

template <class>

class Test

{

public:

void print(T& t);

}

//test.cpp, 需要再次聲明T

template <class>

void Test::print(T&t)

{

std::cout << t << std::endl;

}

高級

二次定義模版

template <class>>/<class>

模板元編程

1.用於編譯開關部分代碼使用

//一個支持透明色的32bit blit函數

template<const>

void blit(int& dst, const int* src, int mask, size_t size)

{

for(size_t = 0; i < size; i++, dst++, src++)

{

if(!useMask || *src != mask)

{

*dst = *src;

}

}

}

void main()

{

//關閉或開啟, 編譯時期確定

blit<true>(....)/<true>

}

  1. 遞歸算法優化
將指數級運算,降低到常數級運算

//被優化算法

unsigned int fib(unsigned int n)

{

if (n == 0 || n == 1) {

return 1;

}

else

{

return fib(n -1) + fib(n-2);

}

}

//優化後算法

template<unsigned>

struct FibR

{

enum

{

Val = FibR::Val + FibR::Val

};

}

//定義模板確定常量時,的格式

template <>

struct FibR<0>

{

enum

{

Val = 1;

}

}

template <>

struct FibR<1>

{

enum

{

Val = 0;

}

}

//定義為一個函數,記得傳遞n

#define fib(n) FibR::Val

可變參數模板

#include <iostream>

void showall()

{

return;

}

template <typename>

void showall(R1 var, Args... args)

{

std::cout << var << std::endl;

showall(args...);

}

int main(int argc, char* args[])

{

//傳遞參數數字

showall(1 ,2 ,3, 4);

//傳遞字符

showall("a", "b", "c");

//混合傳遞

showall(1 ,"abc", 2);

return 0;

}

仿函數中的應用

#include <iostream>

#include <functional>

template <typename>

struct Calc

{

void add(R1 a)

{

std::cout << a <<:endl>

}

void add_1(R1 a, R2 b)

{

std::cout << a + b << std::endl;

}

};

int main(int argc, char* args[])

{

void (Calc::* fc)(int) = &Calc::add;

//實際上不能用

//fc(25);

Calc calc;

auto fun = std::bind(&Calc::add, &calc, std::placeholders::_1);

std::function<void> fun2 = std::bind(&Calc::add_1, &calc, std::placeholders::_1, std::placeholders::_2);/<void>

fun(25);

fun2(1, 25);

return 0;

}

使用using, 函數指針, typedef來實現函數調用

#include <iostream>

#include <stdlib.h>

int calc()

{

return 0;

}

template <typename>

int calc(R1 a, Args... args)

{

return a + calc(args...);

}

int main(int argc, char* args[])

{

int(* fun)(int, int, int) = calc;

std::cout << fun(1, 1, 1) << std::endl;

system("echo 使用函數指針");

typedef int(*Add)(int, int, int);

Add Gadd = calc;

std::cout << Gadd(2, 2, 2) << std::endl;

system("echo 使用typedef實現");

using Func = int(*)(int, int, int);

Func func = calc;

std::cout << func(3, 3, 3) << std::endl;

system("echo 使用using實現");

return 0;

}


分享到:


相關文章: