c++ template模板使用
阿新 • • 發佈:2019-01-31
#include <iostream>
void showall()
{
return;
}
template <typename R1,typename... Args>
void showall(R1 var,Args... args)
{
std::cout << var;
showall(args...);
}
int main(void)
{
showall(1,2,3,4,5);
std::cout << std::endl;
showall("h","h","g");
std ::cout << std::endl;
showall(1.0,1.234,3.5);
std::cout << std::endl;
return 0;
}
- 使用仿函式
仿函式:不是函式但是具有函式功能且用法和函式相同的物件(結構體或者類),一個普通的函式是函式物件,一個函式指標當然也是,廣義上說任何定義了operator()的類物件都可以看作是函式物件。
#include <iostream>
#include <functional>
using namespace std;
using namespace std::placeholders;
template <typename R1,typename R2>
struct Calc
{
void add(R1 a)
{
cout << a << endl;
};
void add_1(R1 a,R1 b)
{
cout << a+b << endl;
};
};
int main(void)
{
Calc<int,int> calc;
auto fun = bind(&Calc<int ,int>::add,&calc,_1);
auto fun_2 = bind(&Calc<int,int>::add_1,&calc,_1,_2);
fun(123);
fun_2(12,24);
return 0;
}
- 使用using別名、函式指標和typedef來實現函式的呼叫
#include <iostream>
int calc()
{
return 0;
}
template <typename R1,typename...Args>
int calc(R1 a,Args...args)
{
return a + calc(args...);
}
int main(void)
{
std::cout << calc(1,2,3,4) << std::endl;
int(*fun)(int,int,int,int)=calc;
std::cout << fun(1,2,3,4) << std::endl;
typedef int(*Add)(int,int,int);
Add Gadd = calc;
std::cout << Gadd(1,2,3) << std::endl;
using Func = int(*)(int,int,int,int);
Func func = calc;
std::cout << func(1,2,3,4) << std::endl;
return 0;
}
- 模板超程式設計
模板超程式設計:在編譯的時候就已經處理完了,只需要在執行的時候輸出結果即可。以斐波那契數列為例
//斐波那契數列
//H(1)=H(0)=1;
//H(N)= H(N-1)+H(N-2);
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define CLK_TCK 1000
using namespace std;
using _int = long;
_int feibona(_int ac)
{
if(ac == 0||ac == 1)
return 1;
return feibona(ac-1) + feibona(ac-2);
}
template <_int N>
struct data
{
enum {res = data<N-1>::res + data<N-2>::res};
};
template <>
struct data<1>
{
enum {res = 1L};
};
template <>
struct data<0>
{
enum {res = 1L};
};
int main(void)
{
time_t a,b;
a = clock();
cout << data<45L>::res << endl;
b = clock();
cout << (double)(b-a)/CLK_TCK << "ms" << endl;
a = clock();
cout << feibona(45L) << endl;
b = clock();
cout << (double)(b-a)/CLK_TCK << "ms" << endl;
return 0;
}
注:實際執行時,很明顯能看出兩種方式的執行效率
//CLK_TCK的值有兩個版本
//版本一:
#define CLK_TCK 18.2
//版本二:
#define CLOCKS_PER_SEC 1000
#define CLK_TCK CLOCKS_PER_SEC
- c++智慧指標
#include <iostream>
#include <memory>
//智慧指標
//std::auto_ptr<double> ptr(new double);
//C++11新的智慧指標
//std::unique_ptr<double> ps(new double);
using namespace std;
/*模式一 分配記憶體地址,而不手動進行回收 */
void showp()
{
for(int i=0;i<10000000;i++)
{
double *p = new double;
}
}
/* 模式二,分配地址,並手動進行回收地址 */
void showp1()
{
for(int i=0;i<10000000;i++)
{
double *p = new double;
delete p;
}
}
/*模式三,分配地址,採用c++通用指標*/
void showp2()
{
for(int i=0;i<10000000;i++)
{
double *p = new double;
auto_ptr<double> ps(p);
}
}
/* 模式四,分配地址,採用C++11新型指標 */
void showp3()
{
for(int i=0;i<10000000;i++)
{
auto_ptr<double> ps(new double);
}
}
int main(void)
{
void(*p[])() = {showp,showp1,showp2,showp3};
p[0]();
p[1]();
p[2]();
p[3]();
return 0;
}
//qt下不知道怎麼檢視memory大小?
智慧指標優勢:不會對一個分配的地址,釋放兩次。如果手動釋放地址,存在著重複釋放或者漏放的情況。 避免記憶體洩露;釋放及時,不會搗鼓電腦中cpu而使電腦運緩慢….