博客
关于我
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实现bellman-ford贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现bellmanFord贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现BellmanFord贝尔曼-福特算法(附完整源码)
    查看>>
    Objective-C实现bezier curve贝塞尔曲线算法(附完整源码)
    查看>>
    Objective-C实现bfs 最短路径算法(附完整源码)
    查看>>
    Objective-C实现BF算法 (附完整源码)
    查看>>
    Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
    查看>>
    Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
    查看>>
    Objective-C实现binary search二分查找算法(附完整源码)
    查看>>
    Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
    查看>>
    Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
    查看>>
    Objective-C实现BinarySearchTreeNode树算法(附完整源码)
    查看>>
    Objective-C实现binarySearch二分查找算法(附完整源码)
    查看>>
    Objective-C实现binomial coefficient二项式系数算法(附完整源码)
    查看>>
    Objective-C实现binomial distribution二项分布算法(附完整源码)
    查看>>
    Objective-C实现bisection二分法算法(附完整源码)
    查看>>
    Objective-C实现bisection二等分算法(附完整源码)
    查看>>
    Objective-C实现BitMap算法(附完整源码)
    查看>>
    Objective-C实现bitmask位掩码算法(附完整源码)
    查看>>
    Objective-C实现bitonic sort双调排序算法(附完整源码)
    查看>>