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

本文共 2822 字,大约阅读时间需要 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/

    你可能感兴趣的文章
    nginx开机启动脚本
    查看>>
    nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    查看>>
    nginx总结及使用Docker创建nginx教程
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
    查看>>
    nginx日志分割并定期删除
    查看>>
    Nginx日志分析系统---ElasticStack(ELK)工作笔记001
    查看>>
    Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
    查看>>
    nginx最最最详细教程来了
    查看>>
    Nginx服务器---正向代理
    查看>>
    Nginx服务器上安装SSL证书
    查看>>
    Nginx服务器的安装
    查看>>
    Nginx模块 ngx_http_limit_conn_module 限制连接数
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx用户认证
    查看>>
    Nginx的location匹配规则的关键问题详解
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>
    Nginx的使用总结(三)
    查看>>
    Nginx的使用总结(二)
    查看>>