c++ stl library 学习(1)

来源:互联网 发布:淘宝新店铺等级怎么看 编辑:程序博客网 时间:2024/05/22 10:41

核心思想:

        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

原创粉丝点击