读书笔记之:C++标准程序库(1)

序言
侯捷译序
C++98标准之后,C++标准库进行了很大的扩充,STL被C++标准库。
侯捷的STL三个阶段:
String,IOSTREAM和Locale程序库
第2章 C++及其标准程序库简介 
string和STL的不同设计原则,IOSTREAM库   
 

模板函数必须在头文件中以inline函数的形式来实现。这是由模板的特点所决定的。   
 

模板参数   
Notype Template参数 (非类型模板参数)   
Default Template参数(缺省模板参数)
异常处理,其中涉及到stack unwinding(堆栈辗转开解)

 

 

 

C++标准异常类别

 

 

 

explicit禁止隐式转换的产生

 

 

 

C++标准手册中的复杂度amortized(分期摊还)

 

 

第3章 一般概念
名字空间的使用
using declaration using声明:引入某个声明   
using directive using指令:是namesapce中的所有名字都曝光
错误处理和异常处理

 

 

 

C++中的标准异常体系

 

 

 

异常所在的头文件

 

 

 

第4章 通用工具Utilities

 

 

 

4.1 Pair
这个类在map和multimap中使用。
需要注意的一个细节是:pair是使用struct定义的,这样所有成员都是public,
pair的部分实现如下:
#include <iostream>
#include <string>
using namespace std;
template <class T1,class T2>
struct _pair{
    T1 first;
    T2 second;

    _pair():first(T1()),second(T2()){
        cout<<"_pair()"<<endl;

    }
    _pair(const T1& a, const T2& b):first(a),second(b){
        cout<<"_pair(const T1& a, const T2& b)"<<endl;
    }
    _pair(const _pair<T1,T2>& p):first(p.first),second(p.second){
        cout<<" _pair(const _pair<T1,T2>& p)"<<endl;
    }

    template<class U,class V>
    _pair(const _pair<U,V>& p):first(p.first),second(p.second){
        cout<<" _pair(const _pair<U,V>& p)"<<endl;
    }
//    bool equal(const _pair<T1,T2>&b){
//        return first==b.first&&second==b.second;
//    }
};
template <class T1,class T2>
inline bool operator==(const _pair<T1,T2>& a,const _pair<T1,T2>& b)
{
//    return a.equal(b);
    return  a.first==b.first&&a.second==b.second;
}

template <class T1,class T2>
inline bool operator<(const _pair<T1,T2>& a,const _pair<T1,T2>& b){
    return a.first<b.first||(!(a.first>b.first)&&(a.second<b.second));
}

template <class T1,class T2>
inline _pair<T1,T2> make__pair(const T1& x,const T2& y){
    return _pair<T1,T2>(x,y);
}
template <class T1,class T2>
inline ostream& operator<<(ostream& outconst _pair<T1,T2>& p){
    out<<"<"<<p.first<<","<<p.second<<">";
}
void f(_pair<int,const char*>& p) {
    cout<<"f()"<<endl;
}
void g(_pair<const int,string>& p) {
    cout<<"g()"<<endl;
}
void f2(_pair<int,const char*> p) {
    cout<<"f2()"<<endl;
}
void g2(_pair<const int,string> p) {
    cout<<"g2()"<<endl;
}

int main(){
    _pair<int,const char*> p(4,"hello");
    cout<<p<<endl;
    f(p);
    f2(p);
    g2(p);
//    g(p);
}

 


 

便捷的make_pair函数

 

 

 

使用make_pair并不会多花你的时间,因为编译器会对这个做一些优化

 

 

 

make_pair产生的类型不具有明确的类型型别。
4.2 智能指针
智能指针的初始化只能使用直接初始化,而不能使用复制初始化。

 

 

posted @ 2012-06-28 00:12  Mr.Rico  阅读(253)  评论(0编辑  收藏  举报