c++ template模板使用

#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而使电脑运缓慢….

转载链接

猜你喜欢

转载自blog.csdn.net/robothj/article/details/80201611