• 验证list的底层数据结构


          《STL源代码剖析》中,指出SGI STL的list底层数据结构式循环双向链表。而且在链表尾端留一个空白节点。让end指向它。因为是双向的,那么list的迭代器必须是Bidirectional Iterator类别的。

           以下。分别验证vs2010下和code blocks(gcc)下。list的底层实现是否是循环链表。

    #include<list>
    #include<iostream>
    
    using namespace std;
    
    int main(){
        list<int> ilist;
    
        for(int i=0;i<3;i++)
            ilist.push_back(i);
    
        cout<<"***********"<<endl;
        int i=0;
        for(list<int>::iterator ite=ilist.begin();i!=15;++i,++ite)
            cout<<*(ite)<<endl;
        cout<<"***********"<<endl;
    
        list<int>::iterator itee=ilist.end();
        cout<<"&&& "<<( ilist.begin()==++itee )<<endl;
    }
    CB下执行结果:


     能够看出。循环遍历list,并输出结果。-2则是空节点中存放的值,正好是-2。


          以上代码在vs2010下可以编译通过,可是执行时会报异常,所迭代器无法dereference,由此可判断,vs在实现list时,是用的双向非循环链表。


  • 相关阅读:
    300. Longest Increasing Subsequence_算法有误
    LIS (DP)_代码
    pthread_detach pthread_create实例
    pthread_detach
    DP(动态规划)
    括号匹配(二)
    gdb调试遇到的问题
    matplotlib 显示中文
    一个奇怪的编码 big5-hkscs
    python 重载 __hash__ __eq__
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7110651.html
Copyright © 2020-2023  润新知