设为首页 加入收藏

TOP

c++ template模板使用实例
2018-05-06 06:06:40 】 浏览:165
Tags:template 模板 使用 实例

通用函数可变参数模板

泛化之美–C++11可变模版参数的妙用

#include 
  
   

void showall()
{
    return;
}

template 
   
     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 
  
   
#include 
   
     using namespace std; using namespace std::placeholders; template 
    
      struct Calc { void add(R1 a) { cout << a << endl; }; void add_1(R1 a,R1 b) { cout << a+b << endl; }; }; int main(void) { Calc
     
       calc; auto fun = bind(&Calc
      
       ::add,&calc,_1); auto fun_2 = bind(&Calc
       
        ::add_1,&calc,_1,_2); fun(123); fun_2(12,24); return 0; }
       
      
     
    
   
  

使用using别名、函数指针和typedef来实现函数的调用

#include 
  
   

int calc()
{
    return 0;
}

template 
   
     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 
  
   
#include 
   
     #include 
    
      #include 
     
       #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
      
       ::res + data
       
        ::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 
  
   
#include 
   
     //智能指针 //std::auto_ptr
    
      ptr(new double); //C++11新的智能指针 //std::unique_ptr
     
       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
      
        ps(p); } } /* 模式四,分配地址,采用C++11新型指针 */ void showp3() { for(int i=0;i<10000000;i++) { auto_ptr
       
         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而使电脑运缓慢….

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++的对象模型,单继承和多继承的.. 下一篇C++里的强制类型转换符reinterpre..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目