c/c++语言开发共享c++ template模板使用实例

通用函数可变参数模板 泛化之美–c++11可变模版参数的妙用 #include void showall() { return; } 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而使电脑运缓慢….

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/608114.html

(0)
上一篇 2021年5月15日
下一篇 2021年5月15日

精彩推荐