2011年11月5日星期六

template

abstraction;
A template class or function can work with many different types
template<typename T>
void print(ostream& out, T data[], int count)
{
   out << "[";
   for (int i = 0; i < count; i++)
   {
      out << data[i];
      if (i + 1 < count)
         out << ",";
   }
   out << "]";
}
以上是模板函数.
另外还有模板类:
template<typename F, typename S>
class Pair
{
public:
   Pair(const F& a, const S& b);
   F get_first() const;
   S get_second() const;
private:
   F first;
   S second;
};

没有评论:

发表评论