zoukankan      html  css  js  c++  java
  • 读书笔记之:C++标准库扩展权威指南

    这本书主要介绍的是C++标准的TR1库。对相关的库都进行了介绍。类似一个参考手册。

    前言

    C++ TR1库简介

    1. 元组tuple
    1. 元组的tuple的声明

    2. 创建tuple对象

    使用make_tuple函数来创建tuple对象,但是make_tuple并不区分对象和对象的引用,两者都会得到和对象相同 类型的成员。

    TR1库中的函数模板ref和cref可以创建包含引用的tuple对象。在头文件<functional>中定义。cref告诉make_tuple所要创建的成员是对常量类型的引用。

    函数模板tie可以创建tuple对象包含对参数的引用。

    程序代码:make_tuple.cc

    View Code
    #include <iostream>
    #include <typeinfo>
    #include <tr1/tuple>
    #include <tr1/functional>
    using std::tr1::tuple;
    using std::tr1::make_tuple;
    using std::tr1::ref;
    using std::tr1::cref;
    using std::tr1::tie;
    using std::tr1::ignore;
    using std::cout;
    using std::endl;
    template <class  T>
    void show_type(T){
        cout<<typeid(T).name()<<endl;
    }
    void test1(){
        int i=3;
        int& j=i;
        show_type(make_tuple());
        show_type(make_tuple(1,3.14));
        show_type(make_tuple(i,j));
    }
    void test2(){
        int i=17;
        int j=3;
        show_type(make_tuple(ref(i),cref(j)));
    }
    void test3(){
        int i=1;
        int j=23;
        int k=-4;
        cout<<i<<' '<<j<<' '<<k<<endl;
        tuple<int,int,int> mytuple;
        mytuple=make_tuple(5,6,7);
        show_type(mytuple);
        tie(i,ignore,k)=mytuple;
        cout<<i<<' '<<j<<' '<<k<<endl;
        show_type(tie(i,ignore,k));

    }
    int main(){
        test3();

        return 0;
    }

    3. tuple的存取

    函数模板get

    程序代码:assign.cc

    View Code
    #include <iostream>
    #include <utility>
    #include <tr1/tuple>
    using namespace std;
    using namespace std::tr1;
    void show(int i,int j,const tuple<int,int&,int>& t){
        cout<<i<<' '<<j<<""
            <<get<0>(t)<<' '
            <<get<1>(t)<<' '
            <<get<2>(t)<<'\n';
    }
    void show(const tuple<int,int>& t){
        cout<<get<0>(t)<<' '
            <<get<1>(t)<<'\n';
    }
    void test1(){
        int i=1,j=2;
        tuple<int,int&,int> t0(i,j,3);
        tuple<int,double,char> t1(4,5.1,'\6');
        show(i,j,t0);
        t0=t1;
        show(i,j,t0);
        tuple<int,int> t2(1,2);
        show(t2);
        t2=make_pair(3,4);
        show(t2);

    }
    void test2(){
        int i=1,j=2;
        tuple<int,int&,int> t0(i,j,3);
        show(i,j,t0);
        get<0>(t0)=4;
        get<1>(t0)=5.1;
        get<2>(t0)='\6';
        show(i,j,t0);
    }
    int main(){
        test2();

        return 0;
    }
     

    4. 类型查询

    当需要知道某个tuple包含了多少元素时,可以使用类模板tuple_size    

    tuple_element获得元素类型

    tuple:http://www.cplusplus.com/reference/std/tuple/tuple/

    第2章 智能指针

    1. shared_ptr和weak_ptr

    这两个没有加入到C++11标准中去。

    shared_ptr类模板

    weak_ptr类模板

    程序源码:

    shared_ptr.cc

    View Code
    #include <tr1/memory>
    #include <iostream>
    using namespace std;
    using namespace tr1;
    void test1(){
        int *ip=new int(3);
        cout<<(void*)ip<<endl;
        shared_ptr<int> sp(ip);
        cout<<(void*)sp.get()<<endl;
        cout<<*sp<<endl;

        cout<<(void*)&*sp<<endl;
    }
    void test2(){
        shared_ptr<int> sp0;
        cout<<"empty object:"<<sp0.use_count()<<" "<<sp0.unique()<<endl;
        shared_ptr<int> sp1((int*)0);
        cout<<"null pointer:"<<sp1.use_count()<<" "<<sp1.unique()<<endl;
        shared_ptr<int> sp2(new int);
        cout<<"one object:"<<sp2.use_count()<<" "<<sp2.unique()<<endl;
        {
            shared_ptr<int> sp3(sp2);
        cout<<"two object:"<<sp3.use_count()<<" "<<sp2.unique()<<endl;
        }
        cout<<"one object:"<<sp2.use_count()<<" "<<sp2.unique()<<endl;
    }
    int main(){
        test2();
        return 0;
    }
     

    第3章 容器基础知识

    第4章 类模板array

    array已经加入到C++11中:http://www.cplusplus.com/reference/stl/array/

    tr1同时为array提供了一些全局的函数

    程序源代码:

    array.cc

    View Code
    #include <tr1/array>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    using namespace std;
    using namespace std::tr1;
    class elt{
        friend ostream& operator<<(ostream& ,const elt&);
        public:
            elt():i(1){}
            elt(int ii):i(ii){}
        private:
            int i;
    };
    ostream& operator<<(ostream& out,const elt& el){
        out<<el.i<<' ';
        return out;
    }
    void test1(){
        array<elt,6> arr0;
        copy(arr0.begin(),arr0.end(),ostream_iterator<elt>(cout," "));
        cout<<endl;
        array<elt,6> arr1={1,2,3,4};
        copy(arr1.begin(),arr1.end(),ostream_iterator<elt>(cout," "));
        cout<<endl;
        array<int,6> arr2={1,2,3,4};
        copy(arr2.begin(),arr2.end(),ostream_iterator<int>(cout," "));
        cout<<endl;
        array<int,6> arr3;
        copy(arr3.begin(),arr3.end(),ostream_iterator<int>(cout," "));
        cout<<endl;
        array<int,6> arr4={};
        copy(arr4.begin(),arr4.end(),ostream_iterator<int>(cout," "));
        cout<<endl;
        array<int,6> arr5=arr2;
        copy(arr5.begin(),arr5.end(),ostream_iterator<int>(cout," "));
        cout<<endl;


    }
    int main(){
        test1();
    }

    第5章 无序关系容器

    这儿其实就是sgiSTL中所实现的散列表。

    已经加入到C++11中:

    unordered_map:http://www.cplusplus.com/reference/stl/unordered_map/

    unordered_multimap:http://www.cplusplus.com/reference/stl/unordered_multimap/

    unordered_multiset:http://www.cplusplus.com/reference/stl/unordered_multiset/

    set: http://www.cplusplus.com/reference/stl/unordered_set/

    关系容器与无序容器

    hash函数

    程序hash.cc

    View Code
    #include <tr1/functional>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    using namespace std;
    using namespace std::tr1;
    template <class T>
    void show_hashes(T first,T last){
        typedef typename iterator_traits<T>::value_type type;
        hash<type> hasher;
        while(first!=last){
            cout<<hasher(*first++)<<' ';
        }
        cout<<endl;
    }
    struct coord{
        int x,y;
    };
    namespace std{
        namespace tr1{
            template <>
            struct hash<coord>{
                std::size_t operator()(const coord& val)const{
                    hash<int> make_hash;
                    return make_hash(val.x)+make_hash(val.y);
                }
            };

        }
    }
    int test1(){
        int data[]={1,2,3,4,5,6};
        show_hashes(data,data+6);
        char* text[]={"1","2","3","4","5"};
        vector<string> strs(text,text+5);
        show_hashes(strs.begin(),strs.end());
        coord points[]={{0,0},{0,1},{1,0},{1,1},{2,2}};
        show_hashes(points,points+5);
        return 0;
    }
    int main(){
        test1();
    }

    第6章 调用包装器基础

    第14章 正则表达式头文件<regex>

    已经加入到标准中

    basic_regex: http://www.cplusplus.com/reference/std/regex/basic_regex/

    第15章 正则表达式语法

    第22章 C语言兼容

  • 相关阅读:
    drf 三大认证详解
    管理表页面的创建
    电脑自动关机设置
    jwt 认证规则
    视图家族练习
    JQuery 数组获取和删除元素
    JQurey 添加和删除元素
    Java 占位符
    Redis
    线程
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2597261.html
Copyright © 2011-2022 走看看