重溫《STL源碼剖析》筆記 第四章

           源碼以前,了無祕密  ——侯傑node

  序列式容器                    關聯式容器         程序員

  array(build in)                  RB-tree架構

  vector                        setdom

    heap                        map函數

      priority-queue                multisetui

  list                          multimapspa

  slist                       hashtablecode

  deque                        hash_setblog

    stack(配接器)                   hash_map接口

    queue(配接器)                  hash_multiset

                             hash_multimap

第四章:序列式容器                      

  C++語言自己提供了一個序列式容器array

  array:分配靜態空間,一旦配置了就不能改變。

  vector:

    分配動態空間。維護一個連續線性空間,迭代器類型爲:Random Access Iterators

    空間配置 typedef simple_alloc<value_type, Alloc> data_allocator

    所謂動態增長空間大小,並非在原空間以後接續新空間,而是以原大小的兩倍另外配置一塊較大

    的空間,而後將原內容拷貝過來,而後纔開始在原內容以後構造新元素,並釋放原空間。所以,

    對vector的任何操做,一旦引發空間從新配置的話,指向原vector的全部迭代器就都失效了,

    這是程序員易犯的一個錯誤。

    可用的方法或函數:begin, end, size, capacity, empty, front, back, find, erase, clear, insert,

    push_back, pop_back

  list:非連續空間

//配置,釋放,構造,銷燬一個節點
protected:
    //配置一個節點並傳回
    link_type get_node() {
        return list_node_allocator::allocate();
    }
    //釋放一個節點
    void put_node(link_type) {
        list_node_allocator::deallocate(p);
    }
    //產生(配置並構造)一個節點,帶有元素值
    link_type create_node(const T& x) {
        link_type p = get_node();
        construct(&p->data,x);    //全局函數,構造/析構基本函數
        return p;
    }
    //銷燬(析構並釋放)一個節點
    void destroy_node(link_type p) {
        destroy(&->data);
        put_node(p);
    }

  remove(const T& value) -> 將數值爲values的全部元素移除

  void list<T,Alloc>::unique() ->移除數值相同的連續元素。注意是「連續」

  list的方法和函數: begin, end, empty, size, front, back,  find, insert, erase, clear, remove, 

  push_front, push_back, pop_front, pop_back, unique, [ sort, splice, merge, reverse -> transfer

 

  deque:雙向開口的連續線性空間,迭代器類型Random Access Iterator, 可是迭代器很是複雜,

  要維護一個地址隊列

  與vector的差別:

      一在於deque能夠常數時間內對起頭端進行插入或移除操做

      二在於deque沒有所謂容量的觀念,動態的以分段連續空間組合而成,隨時能夠增長一段新的

      空間並連接起來。

  deque的最大任務,即是在這些分段的定量連續空間上,維護其總體連續的假象,並提供隨機存取的接口,

  避開了從新配置,複製,釋放的輪迴,代價則是複雜的迭代器架構。

  deque的方法和函數:

  begin,end, empty, size, front, back, find, insert, erase, clear, push_front, push_back,

  pop_front, pop_back, reverse_map_at_back(size-type,node_to_add=1)

  reverse_map_at_front(size_type, node_to_add=1)

 

  stack:沒有迭代器

  方法或函數:empty, size, top, push, pop     stack<int, list<int>> istack

 

  queue:empty, size, front, back, push, pop         queue<int, list<int>> iqueue

  heap:扮演priority queue的助手 使用binary max heap

  方法或函數:push_heap, pop_heap(first, last), sort_heap, make_heap

  

  priority_queue: empty,size, top, push, pop

  slist:單向鏈表

  方法或函數:size,begin,end, find, insert

相關文章
相關標籤/搜索