c++ stl library 学习(1)

核心思想:

        The STL was designed to combine different data structures with different algorithms while achieving the best performance。为了达到最好的效率,不同的数据结构采用不同的算法! 

        Almost all parts of the library are written as templates。几乎所有的lib都是由template写成的。

       template <class T> or template <typename T>  class 和 typename都可以是arbitrary data type。。。建议使用typename更能表达任意的意思。这个数据类型T必须要有template使用的算法。Note that typename is always necessary to qualify an identifier of a template as being a type,。。。typename就是为了说明T是一个数据类型(标准的c++类型或者虚拟的类类型)

      MyClass使用2个参数:

      template <class T, class container = vector<T> >
     class MyClass;

      If you pass only one argument, the default parameter is used as second argument:
      MyClass<int> x1; // equivalent to: MyClass<int,vector<int> >

        

     template <typename T> class MyClass; //使用template的声明


    Member Templates //类函数也可以使用template

    class MyClass {
    ...
    template <class T>
    void f(T);
     };

   template <class T>
class MyClass<T> {
public:
//copy constructor with implicit type conversion
//- does not hide implicit copy constructor
template <class U>
MyClass(const MyClass<U>& x);
...
};
void f()
{
MyClass<double> xd;
...
MyClass<double> xd2(xd); // calls built-in copy constructor 编译器自己为每个类生成的内建的copy constructor
MyClass<int> xi (xd); // calls template constructor                  当类型不一致时,调用的是自己定义的template constructor
...
}


//嵌套的template class

Nested classes may also be templates:
template <class T>
class MyClass {
...

template <class T2>
class NestedClass;
...
};


Exceptions end a call of the function, where you find the exception, with the ability to pass an object as

argument back to the caller.//当异常发生时,它中断函数的执行,然后可以传出一个参数对象给调用者,由事先定义好的catch处理。。。但是我们回不到函数中断的地方继续执行该函数。The throw statement starts a process called stack unwinding; that is, any block or function is left
as if there was a return statement. However, the program does not jump anywhere. For all local
objects that are declared in the blocks that the program leaves due to the exception their
destructors are called. Stack unwinding continues until main() is left, which ends the program,
or until a catch clause "catches" and handles the exception。

 要分清楚exception handling not error handling

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值