
本文共 2561 字,大约阅读时间需要 8 分钟。
���������C++/STL���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������C++������������������������������������������
1. ������������
������
���������������������������������������������������������������������������������������������������������������������������������
templatevoid Swap(T &a, T &b) { T temp = a; a = b; b = temp;}
��������� Swap<int>(5, 3)
��� Swap<double>(1.5, 2.3)
��������������������������� T
��������� int
��� double
���������������������������������
���������������
��������������������������������������������������������������������������������������������������� ���������
void Test() { int a = 1, b = 2; char d = 'a', e = 'b'; double f = 1.5, g = 2.5; Swap(a, b); // ������������������ int ��� Swap(d, e); // ������������������ char ��� Swap(f, g); // ������������������ double ���}
������������������������������������������������������������������������ ���������
void Test() { int a = 1, b = 2; char d = 'a', e = 'b'; double f = 1.5, g = 2.5; Swap (a, e); // ������������������ int Swap(b, d); // ������������������ double}
2. ���������
������
������������������������������������������������������������������������������������������������������������������������������������������������������
templateclass Vector {public: Vector(size_t capacity = 10) : _pData(new T[capacity]), _size(0), _capacity(capacity) {} ~Vector() { if (_pData) { delete[] _pData; _size = _capacity = 0; } } T &operator[](size_t pos) { assert(pos < _size); return _pData[pos]; }private: T * _pData; size_t _size; size_t _capacity;};
���������������������������������������������������������������������������������������������������������������������
void Test() { Vector sq(10); Vectorsq2(10);}
���������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
