C++模板的特性

#include <iostream>
#include <cstdint>

struct CheckValue{
    CheckValue(int a,int b,int c){
    }
    void functions(int a,int b,int c){
       std::cout << a << b << c << std::endl;
    }
};

class CheckValueV2{
public:
    explicit CheckValueV2(const std::string&msg){
        std::cout << "Init" << std::endl;
    }
    int functions(const std::string& msg){
        std::cout << msg << std::endl;
        return 0;
    }
};

//完美转发
template<class T,typename ... Args>
void createT(Args ... args){
   T(std::forward<Args>(args)...).functions(std::forward<Args>(args) ...);
}

int main() {
    createT<CheckValue>(1,2,3);
    createT<CheckValueV2>("hello world");
}

外部接口,使用模板适配

在这里插入图片描述

#include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>

struct CheckValue{
    CheckValue(int a,int b,int c){
    }
    void functions(int a,int b,int c){
       std::cout << a << b << c << std::endl;
    }
};

class CheckValueV2{
public:
    explicit CheckValueV2(const std::string&msg){
        std::cout << "Init" << std::endl;
    }
    int functions(const std::string& msg){
        std::cout << msg << std::endl;
        return 0;
    }
};

//完美转发
template<class T,typename ... Args>
void createT(Args ... args){
   T(std::forward<Args>(args)...).functions(std::forward<Args>(args) ...);
}

template<class T>
class Sum{
private:
       T sum{0};
public:
      void operator() (T b){
           sum = sum + b;
       }
       T get(){
          return sum;
      }
};

int main() {
    createT<CheckValue>(1,2,3);
    createT<CheckValueV2>("hello world");
    std::vector<int> a{1,2,3,4};
    Sum<int> sum = std::for_each(a.begin(),a.end(),Sum<int>());
    std::cout << sum.get() << std::endl;
    return 0;
}

算法匹配

(优化,必须同一个父类才可以调用)父类指针 -> 子类指针

业务代码和算法代码可以进一步优化

C23 标准 为了更好的在C++ 中兼容,引入了C++中的几个关键字 constexpr nullptr 等

在这里插入图片描述

引用他人的截图

别守旧 ,用新的 , stl 库做了好多优化,但是别用最新的

防止窄化转换 尽量使用 {} 进行初始化

严格遵守RAII 原则

不想用Java 开发小程序 是因为封装的太多了,写一句代码有点多,倒不是效率的问题,可读性增加,意味着更多的编码,可以直接使用系统接口,兼容性,更想直接调用linux原始的read 和 write 接口 , 看实际的需求大家都在向NDK 上转,懒才是学习的动力,google 的自动编译和打包的确很方便,还有性能分析工具 … …

在这里插入图片描述

和 python 中的 map 和 filter 方法,python 可是纯C,这工作量不是一般的大

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值