另一个用类型定义的好处就是它允许你去进行代码的协调(这意味着你的代码将是最优化过的),当你用STL的时候这一点尤为正确。
STL允许你在概念上上升到一个更高的水平。如果把一系列约束条件(即一类事物的约束条件)应用于一个类型上那么这个类型就是这类事物基本概念的一个模型。
例如,让我们来看一个封装的概念。Std::vector,std::list,和std::deque都是共享一个界面的封装。
template<class T, class A = allocator<T> >
class Cont {
public:
// 这些是类型定义,也可以看成是界面的一部分 typedef [...] allocator_type;
typedef [...] size_type;
typedef [...] difference_type;
typedef [...] reference;
typedef [...] const_reference;
typedef [...] value_type;
typedef [...] iterator;
typedef [...] const_iterator;
typedef [...] reverse_iterator;
typedef [...] const_reverse_iterator;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
size_type size() const;
size_type max_size() const;
bool empty() const;
A get_allocator() const;
iterator erase(iterator it);
iterator erase(iterator first, iterator last);
void clear();
void swap(Cont x);
};