1.模板
只写一套代码来完成重载功能
(eg:两数比大小,这两个数类型可以是int,char,float)
- 函数模板
template <typename T>
template <class T>
T min(T x,T y)
{ return(x<y)?x:y;}
- 类模板
template <class T>
template <typename T1,typename T2>
void myClass<T1,T2>::show()
{
cout<<"I="<<I<<", J="<<J<<endl;
}
2. 自动类型推断
// 简单自动类型推断
auto a = 123;
cout << "type of a is " << typeid(a).name() << endl;
auto s("fred");
cout << "type of s is " << typeid(s).name() << endl;
// 冗长的类型说明(如迭代器)
vector<int> vec;
auto iter = vec.begin();
cout << "type of iter is " << typeid(iter).name() << endl;