本文共 2822 字,大约阅读时间需要 9 分钟。
???????????????????????????
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++??????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????
?????????
templatevoid Swap(Type &a, Type &b) { Type tem = a; a = b; b = tem;}
????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????double?????????????????????????Type???double?????????????double?????????????????
?????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????a?Type???int?double???????????????Type??????????????Type???int??double??????
??????<>?????????????????a,b??????Type??int???
??????????
templateclass ???? { // ??????};
?????
// ?????templateclass 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; }
?????????????????????????????????<>,???????????<>??????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????
templateT addValue(T const &x) { return x + val;}
?????????????????????????????????????????(class-type)?????????????????????????????????????????
转载地址:http://bvn.baihongyu.com/