GeekBand筆記: STL與泛型編程(容器)

stl

  • stl 6 components
    • allocator
    • container
    • algorithm
    • iterator
    • functor
    • adaptor

sequential container 順序容器

  • elements are stored and accessed sequentially by their position in the container

    • the sequence of elements is corresponding with the position while they adding to the container, not their value.
  • category of sequential container

    • vector
    • deque (double-ended queue)
    • list
    • forward_list (singly linked list)
    • array
    • string
  • sequential container adaptor 順序容器適配器

    • stack
    • queue
    • priority_queue
  • performance trade-offs(性能折中) of container

    • the costs to add/remove elements in container
    • the costs to random access the elements
  • contiguous-memory container (vector, string, array)

    • support fast random access
    • In exchange, add/remove element in the middle of container is inefficient
  • list storage container (list, forward_list)

    • fast to add/remove an element anywhere.
    • In exchange, not support random access
    • Moreover, memory overhead is often substantial. If your program has lots of small elements and space overhead matters, don’t use list or forward_list
  • deque
    • supports fast random access
    • fast insert/delete at front or back
    • 存儲(chǔ)方式待定

associative container 關(guān)聯(lián)容器

  • Elements in an associative container are stored and retrieved by a key

    • In contrast, elements in a sequential container are stored and accessed sequentially by their position in the container.
  • two primary types

    • map

      • The elements in a map are key–value pairs
      • use as dictionary
      • often refer to as an associative array. an associative array is like a normal array except that its subscript don't have to be int.
    • set

      • A set element contains only a key
      • a set is most useful when we simply want to know whether a value is present. for example: white-list
      • use when to keep elements sorted and unique
// elements ordered by key
map // key-value pairs, unique key
set // key is the value, unique key
multimap // allow multiple key
multiset

// unordered key
unordered_map
unordered_set
unordered_multimap
unordered_multiset
  • pair: key-value
    • the data members of pair are public. these members are named first and second
    • In a map, the elements are key-value pairs. That is each element is a pair object.
      • map<T1,T2>::value_type => pair<const T1, T2> //the value type of map is a pair and we can change the value but not the key of pair!
      • map<T1,T2>::key_type => T1
      • map<T1,T2>::mapped_type => T2
pair<T1, T2> p(v1, v2)
pair<T1, T2> p = {v1, v2}
make_pair(v1, v2)

p.first
p.second
  • set<T>::iterator are const

    • the keys in a set are const!, we can use a set iterator to read, but not write.
  • associative container and algorithm

    • In general, we do not use the generic algorithm with associative container. the fact that the keys are const means that we can not pass the associative container iterators to algorithm that write or reorder container elements.
    • associative container can be used with read-only algorithm. however, it is much faster to use the find member(map.find()) than to call generic version.
    • In practice, if we can use an associative container with the algorithm either as source sequence or as destination.
    set<int> c;
    vector<int> v;
    copy(c.begin(), c.end(), back_inserter(v, v.end())) // as a source sequence
    copy(v.begin(), v.end(), inserter(c, c.end())) // as a destination
    

container common operation

typedef in container

common

iterator
const_iterator
size_type
difference_type
value_type
reference
const_reference

associative container

key_type    // type of the key
mapped_type // type associated with each key, map only
value_type // for set, same as key_type; for map, pair<const key_type, mapped_type>

constructor and assignment

common

C c;
C c1(c2); // container type and element type must match
C c(b, e);  // when pass iterators. no requirement that container types are identical, Moreover element type can differ as long as compatible

C c{a,b,c...}; // list initialization.
C c={a,b,c...};

c1 = c2
c1 = {a,b,c...} // replace with the initializer list
a.swap(b) // swap is usually much faster than copy b to a
swap(a,b) //

only for seq container, not valid for associative containers or array

seq.assign(b, e)
seq.assign(il)
seq.assign(n,t)

size

c.size() // forward_list not support
c.max_size()
c.empty()

relational operator

the container type and element type of operands must be identical.

common
==, !=

all except unordered associative container
>, >=, <, <=

add/remove

common, but not support array

c.insert(args)
c.emplace(inits) //???

sequential container add

// add in front or back
c.push_back(t)
c.push_front(t) // only list and deque

// add in anywhere(before the element denoted by iterator p)
c.insert(p, t)  
c.insert(p, n, t)
c.insert(p, b, e) // return an iterator to the first element inserted
c.insert(p, il)

c.emplace_back(args) // args pass to element type's constructor
c.emplace_front(args)
c.emplace(p, args)

associative container add

c.insert(b, e) // iterator range
c.insert(il) // map.insert({word, 1})
.... TODO

remove

common

c.erase(args)
c.clear

sequential container remove

c.pop_back() // return void
c.pop_back()
c.erase(p)
c.erase(b, e) // return an iterator to the element after the last deleted one
c.clear() // return void

access element

sequential container access

c.back() // not for forward_list
c.front()

// ranom access: at() and subscript(operator[]) valid only for contiguous-memory containers
c[n] // no bounds checked, if overrun, undefined error happen. more efficient than at
c.at(n) // bounds checked, if overrun, throw out_of_range exception

notes:

  • make sure the container is not empty, or undefined error happen
  • all access operation return a reference.
  • safe random access
    • operator[] must be "in range [0, size)", or undefined happen.
      • it's up to the programmer to ensure that the index is valid
      • compiler will not check whether the index is out_of_range
    • use at() to ensure the index is invalid
      • if the index is invalid, throw an out_of_range exception

resize a container

c.resize(n) // if n<c.size(), the excess elements are discarded
c.resize(n, t)

capacity

c.shrink_to_fit()
c.capacity()
c.reserve(n)

container operation may invalidate iterator

  • container operations add, remove, or resize, may invalidate pointers, references, or iterators to container elements.
    • it's an serious run-time error to use an invalidated pointers, references, or iterators, as using uninitialized pointer
  • ensure pointers, references, or iterators is refreshed when container is changed by add/remove/resize

選擇合適的容器

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 7月17周一,開始我的減肥日程,第一天減肥,我給自己的目標(biāo)是,跳繩30分鐘,爬樓梯30分鐘,可是跳繩那個(gè)說(shuō)真的,真...
    冬影枕流閱讀 276評(píng)論 0 0
  • 2017.6.29 跟自己做個(gè)約定 希望下一年的自己能夠獨(dú)立去做某件事情,有時(shí)候邁出一步你才會(huì)發(fā)現(xiàn)你自己之前的圈子...
    午間西瓜閱讀 234評(píng)論 0 0
  • 今日關(guān)鍵詞:【優(yōu)秀】 上周四去HG法院開庭,正好趕上法官是該院的副院長(zhǎng)。副院長(zhǎng)親力親為審理本案,一下午2個(gè)多小時(shí)的...
    羅藝律師閱讀 559評(píng)論 0 0

友情鏈接更多精彩內(nèi)容