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
- c++ stl library 学习(1)
- c++ stl library 学习(2)
- c++ stl library 学习(3)
- c++ stl library 学习(4)
- c++ stl library 学习(5)
- c++ stl library 学习(6)
- c++ stl library 学习(7)
- c++ stl library 学习(8)
- c++ stl library 学习(9)
- C Runtime Library 与 STL
- C++STL学习(10)STL算法(1)
- C++STL学习(1)容器vector
- 标准模板库(Standard Template Library,STL)学习
- STL(Standard Template Library)
- C++STL学习(12)STL深入(1) 设计简单的空间配置器
- C++STL学习(11)STL算法(2)
- C++STL学习(2)容器deque
- C++STL学习(3)容器list
- 伪装得再坚强、始终还是掩饰不了内心的脆弱
- 数据库设计三大范式
- 浅谈数据库建表
- 统计入门
- 好久没来了!
- c++ stl library 学习(1)
- 线程下变量-原子操作 __sync_fetch_and_add
- 问心无愧的伤感爱情日志分享:我爱你,到底值不值得
- Linux内核启动过程和Bootloader
- Windows 8 Metro 应用开发入门(一):开发环境介绍
- 何时使用对话框
- 国内手机产业混乱:产业一窝蜂 企业捞快钱
- C#程序 调用第三方DLL控件的一些小启示
- eclipse远程调试tomcat运行的web项目--配置