C++语言 template模板使用实例
小标 2018-07-25 来源 : 阅读 1312 评论 0

摘要:本文主要向大家介绍了C++语言 template模板使用实例,通过具体的内容向大家展示,希望对大家学习C++语言有所帮助。

本文主要向大家介绍了C++语言 template模板使用实例,通过具体的内容向大家展示,希望对大家学习C++语言有所帮助。

通用函数可变参数模板

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

#include <iostream>

 

void showall()

{

    return;

}

 

template <typename args="" typename...="">

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;

}</typename></iostream>

   

使用仿函数

仿函数:不是函数但是具有函数功能且用法和函数相同的对象(结构体或者类),一个普通的函数是函数对象,一个函数指针当然也是,广义上说任何定义了operator()的类对象都可以看作是函数对象。

#include <iostream>

#include <functional>

 

using namespace std;

using namespace std::placeholders;

 

template <typename r2="" typename="">

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;

}</int,int></int,int></int,int></typename></functional></iostream>

   

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

#include <iostream>

 

int calc()

{

    return 0;

}

 

template <typename 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;

}</typename></iostream>

   

模板元编程

模板元编程:在编译的时候就已经处理完了,只需要在运行的时候输出结果即可。以斐波那契数列为例

//斐波那契数列

//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;

}</n-2></n-1></stdio.h></stdlib.h></time.h></iostream>

   

注:实际运行时,很明显能看出两种方式的执行效率

//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大小?</double></double></double></double></memory></iostream>

   

智能指针优势:不会对一个分配的地址,释放两次。如果手动释放地址,存在着重复释放或者漏放的情况。 避免内存泄露;释放及时,不会捣鼓电脑中cpu而使电脑运缓慢….

以上就介绍了C/C+的相关知识,希望对C/C+有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程