博客
关于我
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/

    你可能感兴趣的文章
    Objective-C实现3n+1猜想(附完整源码)
    查看>>
    Objective-C实现3n+1猜想(附完整源码)
    查看>>
    Objective-C实现9x9乘法表算法(附完整源码)
    查看>>
    Objective-C实现9×9二维数组数独算法(附完整源码)
    查看>>
    Objective-C实现A-Star算法(附完整源码)
    查看>>
    Objective-C实现abbreviation缩写算法(附完整源码)
    查看>>
    Objective-C实现ABC人工蜂群算法(附完整源码)
    查看>>
    Objective-C实现activity selection活动选择问题算法(附完整源码)
    查看>>
    Objective-C实现adaboost算法(附完整源码)
    查看>>
    Objective-C实现Adler32算法(附完整源码)
    查看>>
    Objective-C实现AffineCipher仿射密码算法(附完整源码)
    查看>>
    Objective-C实现all combinations所有组合算法(附完整源码)
    查看>>
    Objective-C实现all permutations所有排列算法(附完整源码)
    查看>>
    Objective-C实现all subsequences所有子序列算法(附完整源码)
    查看>>
    Objective-C实现AlphaNumericalSort字母数字排序算法(附完整源码)
    查看>>
    Objective-C实现alternate disjoint set不相交集算法(附完整源码)
    查看>>
    Objective-C实现An Armstrong number阿姆斯特朗数算法(附完整源码)
    查看>>
    Objective-C实现anagrams字谜算法(附完整源码)
    查看>>
    Objective-C实现ApproximationMonteCarlo蒙特卡洛方法计算pi值算法 (附完整源码)
    查看>>
    Objective-C实现area under curve曲线下面积算法(附完整源码)
    查看>>