读书笔记之:Boost程序库完全开发指南(Ch1-4)

第0章 导读

第1章 Boost程序库总论

1.什么是Boost

2. 如何使用Boost

3.什么是STLprot

4.开发环境

第2章 时间与日期

1. timer库

代码如下:

View Code
#include <iostream>
#include <boost/timer.hpp>
using namespace std;
using namespace boost;
int main(){
    timer t;
    cout<<"max timespan:"<<t.elapsed_max()/3600<<"h"<<endl;
    cout<<"min timespan:"<<t.elapsed_min()<<"s"<<endl;
    cout<<"now time elapsed:"<<t.elapsed()<<"s"<<endl;
}

2. progress_timer使用

3. progress_display使用

代码如下:

View Code
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/progress.hpp>
using namespace std;
using namespace boost;
int main(){
    vector<string> v(1000000);
    ofstream fs("a.txt");
    if(!fs){
        cout<<"can't open file a.txt"<<endl;
        return -1;
    }
    progress_display pd(v.size());
    vector<string>::iterator pos;
    for(pos=v.begin();pos!=v.end();++pos){
        fs<<*pos<<endl;
        ++pd;
    }
}

4.date_time库

部分测试代码如下:

View Code
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time.hpp>
#include <algorithm>
#define BOOST_DATE_TIME_SOURCE
using namespace std;
using namespace boost::gregorian;
//using namespace boost;
void test1(){
    date  d(2008,11,20);
//    cout<<to_simple_string(d)<<endl;
//    cout<<to_iso_string(d)<<endl;
//    cout<<to_iso_extended_string(d)<<endl;
    cout<<d<<endl;
//    cin>>d;
//    cout<<d;
}
void test2(){
    date d1(2000,1,1),d2(2008,8,8);
    cout<<d2-d1<<endl;
}
void test3(){
    date d(2008,11,20);
    date d_start(d.year(),d.month(),1);
    date d_end=d.end_of_month();
    for(day_iterator d_iter(d_start);
            d_iter!=d_end;++d_iter){
        cout<<*d_iter<<" "<<
            d_iter->day_of_week()<<endl;
    }
}
void test4(){
    date d(2008,11,20);
    date d18years=d+years(18);
    cout<<d18years<<" is "<<d18years.day_of_week()<<endl;
    int count=0;
    for(day_iterator d_iter(date(d18years.year(),11,1));
            d_iter!=d18years.end_of_month();++d_iter){
        if(d_iter->day_of_week()==Sunday)
            ++count;
    }
    cout<<"total "<<count<<" Sundays."<<endl;
    count=0;
    for(month_iterator m_iter(date(d18years.year(),1,1));
            m_iter<date(d18years.year()+1,1,1);++m_iter){
        count+=m_iter->end_of_month().day();
    }
    cout<<"total "<<count<<" days of year."<<endl;

}
class credit_card{
    public:
        string bank_name;
        int bill_day_no;
        credit_card(const char* bname,int no):
            bank_name(bname),bill_day_no(no){}
        int calc_free_days(date consume_day=day_clock::local_day())const{
            date bill_day(consume_day.year(),consume_day.month(),bill_day_no);
            if(consume_day>bill_day){
                bill_day+=months(1);
            }
            return (bill_day-consume_day).days()+20;

        }
        friend bool operator<(const credit_card& l,const credit_card& r){
            return l.calc_free_days()<r.calc_free_days();
        }
};
void test5(){
    credit_card a("A_bank",25);
    credit_card b("B_bank",12);
    credit_card tmp=std::max(a,b);
    cout<<"You should use "<<tmp.bank_name<<", free days = "<<tmp.calc_free_days()<<endl;
}
int main(){
    test5();
}

5.总结

第3章 内存管理

1. RAII机制

2. 智能指针

3. scoped_ptr

测试代码如下:

View Code
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
struct posix_file{
    posix_file(const char* file_name){
        cout<<"open file:"<<file_name<<endl;
    }
    ~posix_file(){
        cout<<"close file"<<endl;
    }
};
int main(){
    scoped_ptr<int> p(new int);
    if(p){
        *p=100;
        cout<<*p<<endl;
    }
    p.reset();
    assert(p==0);
    if(!p){
        cout<<"scoped_ptr==null"<<endl;
    }
    scoped_ptr<posix_file> fp(new posix_file("a.txt"));
}

 

scope_ptr与auto_ptr的区别

4. scope_array

5. shared_ptr

测试代码:

View Code
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace std;
using namespace boost;
void test1(){
    shared_ptr<int> sp(new int(10));
    cout<<*sp<<endl;
    shared_ptr<int> sp2=sp;
    cout<<*sp<<endl;
    *sp2=100;
    cout<<*sp2<<endl;
}
class shared{
    private:
        shared_ptr<int> p;
    public:
        shared(shared_ptr<int> p_):p(p_){}
        void print(){
            cout<<"count:"<<p.use_count()<<" v="<<*p<<endl;
        }
};
void print_func(shared_ptr<int> p){
    cout<<"count:"<<p.use_count()<<" v="<<*p<<endl;
}
void test2(){
    shared_ptr<int> p(new int(100));
    shared s1(p),s2(p);
    s1.print();
    s2.print();
    *p=20;
    print_func(p);
    s1.print();

}
void test3(){
    shared_ptr<string> sp=make_shared<string>("make_shared");
    cout<<*sp<<endl;
    shared_ptr<vector<int> > spv=make_shared<vector<int> >(10,2);
//    cout<<*spv<<endl;
    cout<<spv->size()<<endl;
}
void test4(){
    typedef vector<shared_ptr<int> > vs;
    vs v(10);
    int i=0;
    for(vs::iterator pos=v.begin();pos!=v.end();++pos){
        *pos=make_shared<int>(++i);
        cout<<*(*pos)<<"";
    }
    cout<<endl;
    shared_ptr<int> p=v[9];
    *p=100;
    cout<<*v[9]<<endl;
}
class sample{
    private:
        class impl;
        shared_ptr<impl> p;
    public:
        sample();
        void print();
};
class sample::impl{
    public:
        void print(){
            cout<<"impl print"<<endl;
        }
};
sample::sample():p(new impl){}
void sample::print(){
    p->print();
}
void test5(){
    sample s;
    s.print();
}
class abstract{
    public:
        virtual void f()=0;
        virtual void g()=0;
    protected:
        virtual ~abstract(){}

};
class impl2:public abstract{
    public:
        void f(){
            cout<<"class impl2 f"<<endl;
        }
        void g(){
            cout<<"class impl2 g"<<endl;
        }
};
class impl3:public abstract{
    public:
        void f(){
            cout<<"class impl3 f"<<endl;
        }
        void g(){
            cout<<"class impl3 g"<<endl;
        }
};
shared_ptr<abstract> create_impl2(){
    return shared_ptr<abstract>(new impl2);
}
shared_ptr<abstract> create_impl3(){
    return shared_ptr<abstract>(new impl3);
}
void test6(){
    shared_ptr<abstract> p2=create_impl2();
    p2->f();
    p2->g();
    shared_ptr<abstract> p3=create_impl3();
    p3->f();
    p3->g();
}

int main(){
    test6();
}

 

6. shared_array

测试代码:

View Code
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(){
    int *p=new int[100];
    shared_array<int> sa(p);
    shared_array<int> sa2=sa;
    sa[0]=10;
    cout<<sa2[0]<<endl;

}

 

7. weak_ptr

测试代码:

View Code
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(){
    shared_ptr<int> sp(new int(10));
    cout<<sp.use_count()<<endl;
    weak_ptr<int> wp(sp);
    cout<<wp.use_count()<<endl;
    if(!wp.expired()){
        shared_ptr<int> sp2=wp.lock();
        *sp2=100;
        cout<<wp.use_count()<<endl;
    }
    cout<<wp.use_count()<<endl;
    sp.reset();
    cout<<wp.expired()<<endl;
    cout<<wp.lock()<<endl;
}

 

8. intrusive_ptr

9. pool库,最简单的pool

测试代码:

View Code
#include <iostream>
#include <boost/pool/pool.hpp>
using namespace std;
using namespace boost;
int main(){
    pool<> pl(sizeof(int));
    int *p=(int*)pl.malloc();
    cout<<pl.is_from(p)<<endl;
    pl.free(p);
    for(int i=0;i<100;i++){
        pl.ordered_malloc(10);
    }
}

 

10. object_pool

测试代码:

View Code
#include <iostream>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
struct demo_class{
    public:
        int a,b,c;
        demo_class(int x=1,int y=2,int z=3):a(x),b(y),c(z){}

};
int main(){
    object_pool<demo_class>pl;
    demo_class *p=pl.malloc();
    cout<<pl.is_from(p)<<endl;
    cout<<p->a<<" "<<p->b<<" "<<p->c<<" "<<endl;
    p=pl.construct(7,8,9);
    cout<<p->a<<" "<<p->b<<" "<<p->c<<" "<<endl;
    object_pool<string> pls;
    for(int i=0;i<10;i++){
        string *ps=pls.construct("hello object_pool",i);
        cout<<*ps<<endl;
    }

}

 

11. singleton_pool

测试代码:

View Code
#include <iostream>
#include <boost/pool/singleton_pool.hpp>
using namespace std;
using namespace boost;
struct pool_tag{};
typedef singleton_pool<pool_tag,sizeof(int)> spl;
int main(){
    int *p=(int*)spl::malloc();
    cout<<spl::is_from(p)<<endl;
    spl::release_memory();
}

12.总结

 

第4章 实用工具

UUID通用唯一标识码

UUID中常用生成器

NIL生成器

字符串生成器

名字生成器

随机数生成器

一个增强的UUID类

UUID与字符串之间的转换

SHA1算法简介

测试代码:

View Code
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/sha1.hpp>
using namespace std;
using namespace boost;
using namespace boost::uuids;
using namespace boost::uuids::detail;
void test1(){
    uuid u;
    cout<<uuid::static_size()<<endl;
    cout<<u.size()<<endl;

    vector<unsigned char> v(16,'A');
    copy(v.begin(),v.end(),u.begin());
    cout<<"v=";
    copy(v.begin(),v.end(),ostream_iterator<unsigned char>(cout," "));
    cout<<endl;
    cout<<"u="<<u<<endl;
    
    fill_n(u.data+10,6,8);

    cout<<u<<endl;
}
void nil_test(){
    uuid u=nil_generator()();
    cout<<u<<endl;
    u=nil_uuid();
    cout<<u<<endl;
}
void string_test(){
    string_generator sgen;
    uuid u1=sgen("0123456789abcdef0123456789abcdef");
    cout<<u1<<endl;
    uuid u2=sgen("01234567-89ab-cdef-0123-456789abcdef");
    cout<<u2<<endl;
    uuid u3=sgen(L"{01234567-89ab-cdef-0123-456789abcdef}");
    cout<<u3<<endl;
}
void name_test(){
    uuid www_xxx_com=string_generator()("{0123456789abcdef0123456789abcdef}");
    name_generator ngen(www_xxx_com);
    uuid u1=ngen("mario");
    cout<<u1.version()<<endl;
    cout<<u1<<endl;
    uuid u2=ngen("link");
    cout<<u2<<endl;
}
void random_test(){
    random_generator rgen;
    uuid u=rgen();
    cout<<u.version()<<endl;
    cout<<u<<endl;
}
class uuid_t:public uuid{
    private:
        static random_generator rgen;
        static string_generator sgen;
    public:
        uuid_t():uuid(rgen()){}
        uuid_t(int):uuid(nil_uuid()){}
        uuid_t(const char* str):uuid(sgen(str)){}
        uuid_t(const uuid& u,const char *str):
            uuid(name_generator(u)(str)){}
        explicit uuid_t(const uuid& u):uuid(u){}
        operator uuid(){
            return static_cast<uuid&>(*this);
        }
        operator uuid()const{
            return static_cast<const uuid&>(*this);
        }
};
random_generator uuid_t::rgen;
string_generator uuid_t::sgen;
void uuid_t_test(){
    uuid_t  u0=0;
    cout<<u0<<endl;
    uuid_t u1,u2;
    cout<<u1<<endl;
    cout<<u2<<endl;

    uuid_t u3("0123456789abcdef0123456789abcdef");
    cout<<u3<<endl;
    cout<<uuid_t(u3,"test name gen")<<endl;

}
void lexical_test(){
    uuid u=lexical_cast<uuid>("01234567-89ab-cdef-0123-456789abcdef");
    cout<<u<<endl;
    string str=lexical_cast<string>(u);
    cout<<str<<endl;
}
void sha1_test(){
    sha1 sha;
    char *szMsg="a short message";
    sha.process_byte(0x10);
    sha.process_bytes(szMsg,strlen(szMsg));
    sha.process_block(szMsg,szMsg+strlen(szMsg));
    unsigned int digest[5];
    sha.get_digest(digest);
    for(int i=0;i<5;i++)
        cout<<hex<<digest[i]<<' ';
    cout<<endl;
}
int main(){
    sha1_test();
}

 

总结:

posted @ 2012-07-06 18:11  Mr.Rico  阅读(...)  评论(...编辑  收藏