for example:
template<class T>
T median(std::vector<T>vec)
{
typedef typername
std::vector<T>size_type vec_sz;
vec_sz size=vec.size();
if(size==0)
throw domain_error();
//后面全部相同
}
不能median(student):要对sort进行重载(加减also).
模板实例化:
在链接期间发生,一般在h文件里面
系统环境要求此模板可访问
五种迭代器:顺序只读(迭代器为const),顺序只写(copy),顺序读写(place),可逆访问(双向访问),随机访问(通过下标进行访问)
顺序只读的访问:
template<class in,class x>
in find(in begin,in end,const X&x)
{
while(begin!=end&&*begin!=x)
++begin;
return begin;
}
//没找到就返回end
顺序只写:
template <class in,class out>
out copy(in begin,in end, out dest)
{
while(begin!=end)
{
if(*begin==x)
*begin=y;
begin++;
}
}
可逆访问:
template <class Bi>
void reverse(Bi begin,Bi end)
{
while(begin!=end)
{
--end;
if(begin!=end)
swap(*begin++,*end);
}
}
随机访问:(二分查找即是) (所以list里面不能用)
标准库:算法设计的思想,迭代器作为算法与容器之间的“粘合剂”从而获得数据结构的独立性