c++ stl library 学习(3)

来源:互联网 发布:贪吃蛇算法 编辑:程序博客网 时间:2024/06/14 15:30

  C++STL中所有的identifiers都定义在namespace std中。

 STL 头文件,采用没有扩展名的方式引用头文件,eg,#include<vector>

   c语言同样采用在加上前缀c的方式,eg,#include<cstring>的方式。//was #include<string.h>

  无扩展名只适应于标准头文件。如果是你自己写的头文件最好加上.h。

#include<stdlib.h> #include<iostream.h>为old head filer.仍然可以使用,不过它们中的identifier在全局有效,不需要在特别声明使用某个包含的头文件。


explicit 的构造函数,禁止隐式转换,但是允许显示转换。eg:

class A{

explicit A( int len);

explicit  A(const A& a){ }; //copy construct ,当有新的对象产生时调用;

explicit A& operator= (const A& a){};copy assignment 没有新的对象产生的赋值运算才调用

};

class B{

explicit B(A aa);

int sort(int* a) const;//const 成员函数
任何不会修改数据成员的函数都应该声明为const 类型。如果在编写const 成员函数时,不慎修改了数据成员,或者调用了其它非const 成员函数,编译器将指出错误,这无疑会提高程序的健壮性

};

A a1(1); //ok

B b(1);//   error

B b( A(1)); // ok


异常是指 一个事件可能发生但是不能被我们控制而产生的事件。。。

而error handing则是,我们可以控制的事件,但是我们努力做好规避动作。。。


RTTI(Run-Time Type Identification,通过运行时类型识别)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。。。

如何确定对象的动态类型呢?答案是使用内建的 RTTI 中的运算符:typeid 和 dynamic_cast


//copy constructor with implicit conversions//template constructor 
template<class U, class V>
pair(const pair<U,V>& p)
: first(p.first), second(p.second) {
}

void f(std::pair<int,const char*>);
void g(std::pair<const int.std::string>);
...
void foo {
std::pair<int,const char*> p(42,"hello");
f(p); //OK: calls built-in default copy constructor
g(p); //OK: calls template constructor
}

make_pair()//The make_pair() template function enables you to create a value pair without writing the types
f(make_pair(42,"hello"));  //
g(make_pair(42,"hello"); // make_pair返回一个pair类型,然后通过template constructor将pair进行隐式转换成目标参数类型的pair。
explicitly


原创粉丝点击