C++ 标准模板库组件介绍

来源:互联网 发布:淘宝上的经典搞笑好评 编辑:程序博客网 时间:2024/04/27 15:53

原文地址:http://my.oschina.net/myspaceNUAA/blog/78557

 

在前几天的阿里面试过程中,问到了我标准模板库的继承体系。平时开发对vector,list, map,set ,stack等容器用的比较多,但是没有深入研究过。经历过面试,发现了很多需要完善和提高的地方。
但是有个问题哦,标准模板库中得几大组件没有啥继承关系,只是说有某些容器之间有适配关系。

Container(容器):

所谓容器,就是存放数据的仓库,定义了数据在内存中的组织方式,
主要:有序列式容器(线性数据结构)、关联连式容器(非线性数据结构:树结构)
序列容器,典型的容器vector,相对于C++内嵌的数组,vector的优点是支持数据的动态扩展。
而空间的动态扩展,有空间配置器配置。它会为容器提供空间分配的策略,比如空间不够时增长的幅度。

关联式容器:关联式容器的思想类似于关系数据库,由key-value组成。 主要分为map和set,hash及其相关衍生.

Iteraotrs(迭代器):

迭代器统一了容器的访问操作,很好的东西。想到了迭代器模式
view source
print?
1Container<Type> cons;
2for (Container<Type>::iterator iter = cons.begin();
3     iter != cons.end(); iter++)
4{
5    visit(iter);
6}

看看《STL源码分析》中关于vector的定义
view source
print?
1template<classT,class Alloc = alloc>
2class vector
3{
4    typedefT value_type;
5    typedefvalue_type* pointer;
6    typedefvalue_type* iterator;
7    ......
8}

这里很明显的是通过typedef定义将指针定义为vector的迭代器,提供统一访问,但是本质上还是指针哈。

VC的vector的迭代器定义:是一个类哈,有许多数据组成了迭代器的定义,其中有指针,有引用。

view source
print?
01template<class_Myvec>
02    class_Vector_iterator
03        :public_Vector_const_iterator<_Myvec>
04    {   // iterator for mutable vector
05public:
06    typedef_Vector_iterator<_Myvec> _Myiter;
07    typedef_Vector_const_iterator<_Myvec> _Mybase;
08    typedefrandom_access_iterator_tag iterator_category;
09 
10    typedeftypename_Myvec::value_type value_type;
11    typedeftypename_Myvec::difference_type difference_type;
12    typedeftypename_Myvec::pointer pointer;
13    typedeftypename_Myvec::reference reference;
14        ......
15}

pointer定义,就是某个数据类型的指针

view source
print?
1typedef value_type _FARQ *pointer;

再看下hashtable中的迭代器的定义:

view source
print?
1......
2struct _hashtable_iterator
3{
4    ......
5    node* cur;
6    hashtable* ht;
7    .....
8}

在hashtable迭代器中,有两个指针,一是当前节点的指针,二是buckets vector的指针。

综上所述,所谓迭代器,就是对指针以及其他辅助信息的一个封装。对数据的访问一定是通过地址访问,地址是必须的,所以地址是迭代器中不可缺少的信息。
在迭代器的使用中,常常遇到的一个问题就是,在进行数据的插入删除时的失效问题!其实就是指针的问题哈

Allocator(空间配置器):

空间配置器用于屏蔽容器关于内存管理的细节。比如容器内存的申请释放,当内存不够时采用怎样的一种策略。在我们平常的使用中,

view source
print?
1vector<strudent> stus;

并没有制定容器的空间配置方案,于是采用默认的配置方案,其实我们是可以自己编写空间配置方案,并将该方案实施于某个容器。(CustomAlloc是自定义的命名空间,并实现了空间配置方案allocator)
view source
print?
1vector<int, CustomAlloc::allocator<int>> datas;

可以在自定义的方案中进行内存管理。

Algorithms(算法):

提供了大量常用、通过的算法,比如比较、查询、数据移动、复制、交换等等。
基础算法:min, max, swap
排序:sort
替换:replace
查找:find
此处不一一列举

Function Objects(函数对象):

函数对象,简单的理解就是将一个函数封装为对象,但是它的作用是为容器的操作提供依据。我进行比较大小,根据什么比,函数对象提供,查询,匹配的规则是什么,函数对象提供。 
例如:
1. 对vector容器进行排序,排序的标准是?
2. 对容器中得数据进行查询,查询的标准是?
通过函数对象,可以灵活的编写操作依据,并注入到操作函数中。
为sort函数编写Compare()

为find_if编写Query()

此处写了个实例,说明一下用法:
业务对象定义:

view source
print?
01class student
02{
03public:
04        string name;
05        intage;
06 
07        student(string name,intage)
08        {
09             this->name = name;
10             this->age = age;
11        }
12 
13        student(conststudent &stu)
14        {
15                *this= stu;
16        }
17 
18        student& operator=(conststudent &stu)
19        {
20            this->name = stu.name;
21            this->age = stu.age;
22            return*this;
23        }
24};

函数对象定义,说明对象之间比较的依据,此处依据是年龄
view source
print?
01struct Compare : public std::binary_function<student, student, bool>
02{
03    booloperator()(conststudent stu1,conststudent stu2)const
04    {
05            if(stu1.age < stu2.age)
06                returntrue;
07            else
08                returnfalse;
09    }
10};

测试程序:
view source
print?
01int _tmain(int argc, _TCHAR* argv[])
02{
03    vector<student> stus;
04    srand((unsignedint)time(NULL));
05    string strName ="student";
06    for(inti = 0; i < 10; i++)
07    {
08        intval =rand()/10;
09        chardataBuf[20];
10        memset(dataBuf, 0, 20);
11        itoa(val, dataBuf, 10);
12        student stu(dataBuf, val);
13        stus.push_back(stu);
14    }
15 
16    for(vector<student>::iterator iter = stus.begin(); iter != stus.end(); iter++)
17        cout<<iter->name<<"--"<< iter->age<<endl;
18    cout<<"-----------------------------"<<endl;
19    sort(stus.begin(), stus.end(),  Compare());
20    for(vector<student>::iterator iter = stus.begin(); iter != stus.end(); iter++)
21        cout<<iter->name<<"--"<< iter->age<<endl;
22 
23    getchar();
24    return0;
25}

测试结果:将原本随机插入的数据根据年龄进行了排序

注:此处是对几个组件的简要说明,并未详细深入

原创粉丝点击