理解函数模板
#include<iostream>
using namespace std;
template<typename T>
bool com(T a, T b)
{
cout << "template compare" << endl;
return a > b;
}
template<>
bool com(const char* a, const char* b)
{
cout << "compare <const char*> " << endl;
return strcmp(a,b)>0;
}
template<typename T, typename E>
bool compare(T a, T b)
{
cout << "template compare" << endl;
return a > b;
}
int main()
{
compare<int,int>(10, 20);
compare<double, int>(10, 20.0);
com(10, 20);
com<double>(10, 23.5);
com("aaa", "bbb");
com<const char*>("aaa", "bbb");
return 0;
}
非类型参数
#include<iostream>
#include <algorithm>
using namespace std;
template<typename T,int SIZE>
void sort(T* arr)
{
for (int i = 0; i < SIZE - 1; ++i)
{
for (int j = 0; j < SIZE - 1 - i; ++j)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int arr[] = { 12,23,14,25,34,67,10,2 };
const int size = sizeof(arr) / sizeof(arr[0]);
sort<int,size>(arr);
for (int val : arr)
{
cout << val << " "<< endl;
}
}
类模板
#include<iostream>
using namespace std;
template<typename T = int>
class SeqStack
{
public:
SeqStack<T>(int size = 10)
:_pstack(new T[size])
,_top(-1)
,_size(size)
{
}
~SeqStack<T>()
{
delete[] _pstack;
_pstack = nullptr;
}
SeqStack<T>(const SeqStack<T>& stack)
:_top(stack._top)
,_size(stack._size)
{
_pstack = new T[_size];
for (int i = 0; i < top; ++i)
{
_pstack[i] = stack._pstack[i];
}
}
SeqStack<T>& operator=(const SeqStack<T>& stack)
{
if (this == &stack)
{
return *this;
}
delete[] _pstack;
_top = stack._top;
_size = stack._size;
_pstack = new T[_size];
for (int i = 0; i < top; ++i)
{
_pstack[i] = stack._pstack[i];
}
}
void push(const T& val)
{
if (full())
{
expand();
}
_pstack[top++] = val;
}
void pop()
{
if (empty())
{
return;
}
--top;
}
T top()const
{
if (empty())
{
throw "stack is empty!";
}
return _pstack[_top - 1];
}
bool full()const
{
return _top == _size;
}
bool empty()const
{
return _top == 0;
}
private:
T* _pstack;
int _top;
int _size;
void expand()
{
T* ptmp = new T[2 * _size];
for (int i = 0; i < top; ++i)
{
_pstack[i] = stack._pstack[i];
}
delete[] _pstack;
_pstack = ptmp;
_size = _size * 2;
}
};
template<typename T>
void SeqStack<T>::push(const T& val)
{
if (full())
{
expand();
}
_pstack[_top++] = val;
}
int main()
{
return 0;
}
实现简易C++ STL向量容器Vector
#include<iostream>
using namespace std;
template<typename T>
class vector
{
public:
vector(int size = 10)
{
_first = new T[size];
_last = _first;
_end = _first + size;
}
~vector()
{
cout <<" 析构对象是" <<this->name << endl;
delete[] _first;
}
vector(const vector<T>& src)
{
int len = src._last - src._first;
_first = new T[src._end - src._first];
for (int i = 0; i < len; ++i)
{
_first[i] = src._first[i];
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator= (const vector<T>& src)
{
if (this == &src)
{
return *this;
}
delete[] _first;
int len = src._last - src._first;
_first = new T[src._end - src._first];
for (int i = 0; i < len; ++i)
{
_first[i] = src._first[i];
}
_last = _first + len;
_end = _first + size;
return *this;
}
void push_back(const T &val)
{
if (full())
expand();
*_last++ = val;
}
void pop_back()
{
if (empty())
{
return;
}
_last--;
}
T back() const
{
return *(_last - 1);
}
void show() { cout << "This is" <<name<< endl; }
bool full() const { return _last == _end; }
bool empty() const { return _last == _first; }
int size() const{ return _last - _first; }
int name;
private:
T* _first;
T* _last;
T* _end;
void expand()
{
int size = _end - _first;
T* ptmp = new T[2 * size];
for (int i = 0; i < size; ++i)
{
ptmp[i] = _first[i];
}
_first = ptmp;
_last = _first + size;
_end = _first + 2 * size;
}
};
int main()
{
vector<int>vec;
vec.name = 0;
for (int i = 0; i < 20; ++i)
{
vec.push_back(rand() % 100);
}
while (!vec.empty())
{
cout << vec.back() << endl;
vec.pop_back();
}
return 0;
}
空间配置器
#include<iostream>
using namespace std;
template <typename T>
class Allocator
{
public:
Allocator()
{
}
~Allocator()
{
}
T* allocate(size_t size)
{
return (T*)malloc(sizeof(T) * size);
}
void deallocate(void* p)
{
free(p);
}
void construct(T* p, const T& val)
{
new(p)T[val];
}
void destroy(T* p)
{
p->~T();
}
};
template<typename T,typename Alloc = Allocator<T>>
class vector
{
public:
vector(int size = 10,const Alloc &alloc = Allocator<T>())
:_allocator(alloc)
{
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector()
{
for (T* p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = _last = _end = nullptr;
}
vector(const vector<T>& src)
{
int len = src._last - src._first;
int size = src._end - src._first;
_first = _allocator.allocate(size);
for (int i = 0; i < size; ++i)
{
_allocator.construct(_first + i, src._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator= (const vector<T>& src)
{
if (this == &src)
{
return *this;
}
int len = src._last - src._first;
for (T* p = _first; p != _last; ++p)
{
_allocator.destory(p);
}
_allocator.deallocate(_first);
int len = src._last - src._first;
int size = src._end - src._first;
_first = _allocator.allocate(size);
for (int i = 0; i < size; ++i)
{
_allocator.construct(_first + i, src._first[i]);
}
_last = _first + len;
_end = _first + size;
return *this;
}
void push_back(const T& val)
{
if (full())
expand();
_allocator.construct(_last, val);
_last++;
}
void pop_back()
{
if (empty())
{
return;
}
_last--;
_allocator.destroy(_last);
}
T back() const
{
return *(_last - 1);
}
void show() { cout << "This is" << name << endl; }
bool full() const { return _last == _end; }
bool empty() const { return _last == _first; }
int size() const { return _last - _first; }
int name;
private:
T* _first;
T* _last;
T* _end;
Alloc _allocator;
void expand()
{
int size = _end - _first;
T* ptmp = _allocator.allocate(2 * size);
for (int i = 0; i < size; ++i)
{
_allocator.construct(ptmp + i, _first[i]);
}
for (T* p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = ptmp;
_last = _first + size;
_end = _first + 2 * size;
}
};
class Test
{
public:
Test() { cout << "Test()" << endl; }
~Test() { cout << "~Test()" << endl; }
Test(const Test&) { cout << "Test(const Test&)" << endl; }
private:
};
int main()
{
vector<Test>vec;
Test T1, T2, T3;
cout << "_________________________" << endl;
vec.push_back(T1);
vec.push_back(T2);
vec.push_back(T3);
cout << "_________________________" << endl;
vec.pop_back();
cout << "_________________________" << endl;
}