博客
关于我
C++ 模板初阶(函数模板&类模板),非类型模板参数,零初始化
阅读量:99 次
发布时间:2019-02-25

本文共 2770 字,大约阅读时间需要 9 分钟。

????

1. ??-????

???????????????????????????

void Swap(int &a, int &b) {    int tem = a;    a = b;    b = tem;}void main() {    int a = 1, b = 2;    char c = 'A', d = 'B';    double e = 1.1, f = 2.2;    cout << "???:a=" << a << "  b=" << b << endl;    Swap(a, b);    cout << "???:a=" << a << "  b=" << b << endl;    cout << "???:c=" << c << "  d=" << d << endl;    Swap(c, d);    cout << "???:c=" << c << "  d=" << d << endl;    cout << "???:e=" << e << "  f=" << f << endl;    Swap(e, f);    cout << "???:e=" << e << "  f=" << f << endl;}

??????????????????????????????

  • ????????????????????????????????????????????
  • ?????????????????????????
  • ???????????????????????????????????????????

    C++??????????????????????????????????????????????????????????????????????????????

    2. ????

    2.1 ???????

    ????????????????????????????????????????????????????

    2.2 ???????

    ?????????

    template
    void Swap(Type &a, Type &b) { Type tem = a; a = b; b = tem;}

    2.3 ???????

    ????????????????????????????????????????????????????????????????

    ??????????????????????????????????????????????????????double?????????????????????????Type???double?????????????double?????????????????

    2.4 ????????

    ?????????????????????????????????????????????????

    2.4.1 ?????

    ?????????????????????????????????????????????????????????????????????????????a?Type???int?double???????????????Type??????????????Type???int??double??????

    2.4.2 ?????

    ??????<>?????????????????a,b??????Type??int???

    2.5 ?????????

  • ??????????????????????????????????????????????
  • ????????????????????????????????????????????????????????????????????????????????
  • ?????????????????????????????
  • 3. ???

    3.1 ????????

    ??????????

    template
    class ???? { // ??????};

    ?????

    // ?????template
    class Vector {public: Vector(size_t capacity = 10) : _pData(new T[capacity]) , _size(0) , _capacity(capacity) {} ~Vector(); void PushBack(const T &data); void PopBack(); // ... size_t Size() { return _size; } T &operator[](size_t pos) { assert(pos < _size); return _pData[pos]; }private: T *_pData; size_t _size; size_t _capacity;};template(class t)Vector
    :: ~Vector() { if (_pData) delete[] _pData; _size = _capacity = 0; }

    3.2 ???????

    ?????????????????????????????????<>,???????????<>??????????????????????????????

    4. ???????

    4.1 ????????????

    ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

    4.2 ??????????

    ??????????????????????????????????????????

    template
    T addValue(T const &x) { return x + val;}

    4.3 ??????????

    ?????????????????????????????????????????(class-type)?????????????????????????????????????????

    转载地址:http://bvn.baihongyu.com/

    你可能感兴趣的文章
    Netty核心模块组件
    查看>>
    Netty源码—4.客户端接入流程一
    查看>>
    Netty源码—5.Pipeline和Handler一
    查看>>
    Netty源码—6.ByteBuf原理二
    查看>>
    Netty源码—7.ByteBuf原理三
    查看>>
    Netty源码—7.ByteBuf原理四
    查看>>
    Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
    查看>>
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>
    Network Sniffer and Connection Analyzer
    查看>>
    Nginx Location配置总结
    查看>>
    Nginx 反向代理解决跨域问题
    查看>>
    nginx 后端获取真实ip
    查看>>
    Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
    查看>>
    nginx 常用配置记录
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>