目录
- 一、函数模板
- 二、类模板
- 三、实现C++ STL向量容器vector代码
- 四、理解容器空间配置器allocator的重要性
一、函数模板
1、模板的意义:对类型也可以进行参数化了
2、函数模板<=是不进行编译的,因为类型还不知道
模板的实例化<=函数调用点进行实例化
模板函数<=要被编译器所编译的
3、模板类型参数typename/class
模板非类型参数
4、模板的实参推演=>可以根据用户传入的实参类型,来推导出模板类型的具体类型
模板的特例化(专用化):特殊(不是编译器提供的,而是用户提供的)的实例化
函数模板、模板的特例化、非模板函数的重载关系
5、模板代码是不能在一个文件中定义,在另一个文件中使用的,否则链接的时候会出现错误。
模板代码调用之前,一定要看到模板定义的地方,这样的话,模板才能进行正常的实例化,产生能够被编译器编译的代码。
所以,模板代码都是放在头文件当中的,然后在原文件当中直接进行#include
包含
#include <iostream>
using namespace std;
template<typename T>
bool compare(T a, T b)
{
cout << "template compare" << endl;
return a > b;
}
template<>
bool compare(const char* a, const char* b)
{
cout << "compare<const char*>" << endl;
return strcmp(a, b) > 0;
}
bool compare(const char* a, const char* b)
{
cout << "normal compare" << endl;
return strcmp(a, b) > 0;
}
int main()
{
compare<int>(10, 20);
compare<double>(10.5, 20.5);
compare(20, 20);
compare("aaa", "bbb");
compare<const char*>("aaa","bbb");
return 0;
}
编译器优先把compare处理成函数名字,没有的话,才去找compare模板特例化,还没有才找模板实例化。这样做的事情最少。
6、模板的非类型参数:必须是整数类型(整数或者地址/引用都可以)是常量,只能使用,而不能修改
#include <iostream>
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 tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[] = { 12,3,4,56,7,89,0 };
const int size = sizeof(arr) / sizeof(arr[0]);
sort<int, size>(arr);
for (int val : arr)
{
cout << val << " ";
}
cout << endl;
return 0;
}
二、类模板
类模板=>实例化=>模板类
构造和析构函数名不用加,其他出现模板的地方都加上类型参数列表
#include <iostream>
using namespace std;
template<typename T>
class SeqStack
{
public:
SeqStack(int size = 10)
:_pstack(new T[size])
,_top(-1)
,_size(size)
{}
~SeqStack()
{
delete[]_pstack;
_pstack = nullptr;
}
SeqStack(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;
_pstack = new T[_size];
for (int i = 0; i < _top; i++)
{
_pstack[i] = stack._pstack[i];
}
return *this;
}
void push(const T& val);
void pop()
{
cout << "pop():"<<_pstack[_top] << endl;
if (empty())
return;
--_top;
}
T top()
{
if (empty())
throw "stack is empty";
return _pstack[_top];
}
bool full() { return _top == _size - 1; }
bool empty() { return _top == -1; }
private:
T* _pstack;
int _top;
int _size;
void expand()
{
T* ptmp = new T[_size * 2];
for (int i = 0; i < _top; i++)
{
ptmp[i] = _pstack[i];
}
delete[]_pstack;
_pstack = ptmp;
_size *= 2;
}
};
template<typename T>
void SeqStack<T>::push(const T& val)
{
cout << "push(const T & val):"<<val << endl;
if (full())
expand();
_pstack[++_top] = val;
}
int main()
{
SeqStack<int> s1;
s1.push(20);
s1.push(10);
s1.push(13);
s1.push(35);
s1.pop();
cout << s1.top() << endl;
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()
{
delete[]_first;
_first = _last = _end = nullptr;
}
vector(const vector<T>& rhs)
{
int size = rhs._end - rhs._first;
_first = new T[size];
int len = rhs._last - rhs._first;
for (int i = 0; i < len; i++)
{
_first[i] = rhs._first[i];
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator=(const vector<T>& rhs)
{
if (this == &rhs)
return*this;
delete[]_first;
int size = rhs._end - rhs._first;
_first = new T[size];
int len = rhs._last - rhs._first;
for (int i = 0; i < len; i++)
{
_first[i] = rhs._first[i];
}
_last = _first + len;
_end = _first + size;
}
void push_back(const T& val)
{
if (full())
expand();
*_last++ = val;
cout << val << " ";
}
void pop_back()
{
if (empty())
return;
_last--;
}
T back()const
{
return *(_last - 1);
}
bool full() { return _last == _end; }
bool empty() { return _last == _first; }
int size() { return _last - _first; }
private:
T* _first;
T* _last;
T* _end;
void expand()
{
int size = _end - _first;
T* ptmp = new T[size * 2];
for (int i = 0; i < size; i++)
{
ptmp[i] = _first[i];
}
delete[]_first;
_first = ptmp;
_last = _first + size;
_end = _first + size * 2;
}
};
int main()
{
vector<int> vec;
for(int i=0;i<20;i++)
{
vec.push_back(rand() % 100);
}
cout << endl;
vec.pop_back();
while (!vec.empty())
{
cout << vec.back() << " ";
vec.pop_back();
}
return 0;
}
运行结果:
四、理解容器空间配置器allocator的重要性
容器的空间配置器allocator做四件事:内存开辟、内存释放;对象构造、对象析构
#include <iostream>
using namespace std;
template<typename T>
struct 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)
{
_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>& rhs)
{
int size = rhs._end - rhs._first;
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; i++)
{
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
}
vector<T>& operator=(const vector<T>& rhs)
{
if (this == &rhs)
return*this;
for (T* p = _first; p != _last; ++p)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
int size = rhs._end - rhs._first;
_first = _allocator.allocate(size);
int len = rhs._last - rhs._first;
for (int i = 0; i < len; i++)
{
_allocator.construct(_first + i, rhs._first[i]);
}
_last = _first + len;
_end = _first + size;
}
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);
}
bool full() { return _last == _end; }
bool empty() { return _last == _first; }
int size() { return _last - _first; }
private:
T* _first;
T* _last;
T* _end;
Alloc _allocator;
void expand()
{
int size = _end - _first;
T* ptmp = _allocator.allocate(size * 2);
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 + size * 2;
}
};
class Test
{
public:
Test() { cout << "Test()" << endl; }
~Test() { cout << "~Test()" << endl; }
Test(const Test&) { cout << "Test(const Test&)" << endl; }
};
int main()
{
Test t1, t2, t3;
cout << "------------------------" << endl;
vector<Test> vec;
cout << "------------------------" << endl;
vec.push_back(t1);
vec.push_back(t2);
vec.push_back(t3);
cout << "------------------------" << endl;
vec.pop_back();
cout << "------------------------" << endl;
return 0;
}
运行结果:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)