STL概述
来源:互联网 发布:jsp与javascript 知乎 编辑:程序博客网 时间:2024/05/16 11:08
STL = Standard Template Library,标准模板库。包含容器(Container)、迭代器(Iterator)、算法(algorithm)。
一、容器
Sequence containers(序列容器)
Sequence containers implement data structures which can be accessed sequentially.
static contiguous array
(class template) dynamic contiguous array
(class template) double-ended queue
(class template) forward_list
(since C++11)
singly-linked list
(class template) doubly-linked list
(class template) Associative containers(关联容器)
Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).
collection of unique keys, sorted by keys
(class template) collection of key-value pairs, sorted by keys, keys are unique
(class template) collection of keys, sorted by keys
(class template) collection of key-value pairs, sorted by keys
(class template)Unordered associative containers(未排序关联容器)
Associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized,O(n) worst-case complexity).
unordered_set
(since C++11)
collection of unique keys, hashed by keys
(class template) unordered_map
(since C++11)
collection of key-value pairs, hashed by keys, keys are unique
(class template) unordered_multiset
(since C++11)
collection of keys, hashed by keys
(class template) unordered_multimap
(since C++11)
collection of key-value pairs, hashed by keys
(class template)Container adaptors(容器适配器)
Container adaptors provide a different interface for sequential containers.
adapts a container to provide stack (LIFO data structure)
(class template) adapts a container to provide queue (FIFO data structure)
(class template) adapts a container to provide priority queue
(class template)Member map
This is a comparison chart with the different member functions present on each of the different containers:
Sequence containers | Associative containers | | Headers<vector> | <deque> | <list> | <set> | <map> | <bitset> | Memberscomplexvector | deque | list | set | multiset | map | multimap | bitset | constructor*constructorconstructorconstructorconstructorconstructorconstructorconstructorconstructordestructorO(n)destructordestructordestructordestructordestructordestructordestructor operator=O(n)operator= operator= operator= operator= operator= operator= operator= operatorsiteratorsbeginO(1)beginbeginbeginbeginbeginbeginbegin endO(1)endendendendendendend rbeginO(1)rbeginrbeginrbeginrbeginrbeginrbeginrbegin rendO(1)rendrendrendrendrendrendrend capacitysize*sizesizesizesizesizesizesizesizemax_size*max_sizemax_sizemax_sizemax_sizemax_sizemax_sizemax_size emptyO(1)emptyemptyemptyemptyemptyemptyempty resizeO(n)resizeresizeresize element accessfrontO(1)frontfrontfront backO(1)backbackback operator[]*operator[] operator[] operator[] operator[] atO(1)atat modifiersassignO(n)assignassignassign insert*insertinsertinsertinsertinsertinsertinsert erase*eraseeraseeraseeraseeraseeraseerase swapO(1)swapswapswapswapswapswapswap clearO(n)clearclearclearclearclearclearclear push_frontO(1) push_frontpush_front pop_frontO(1) pop_frontpop_front push_backO(1)push_backpush_backpush_back pop_backO(1)pop_backpop_backpop_back observerskey_compO(1) key_compkey_compkey_compkey_comp value_compO(1) value_compvalue_compvalue_compvalue_comp operationsfindO(log n) findfindfindfind countO(log n) countcountcountcountcountlower_boundO(log n) lower_boundlower_boundlower_boundlower_bound upper_boundO(log n) upper_boundupper_boundupper_boundupper_bound equal_rangeO(log n) equal_rangeequal_rangeequal_rangeequal_range unique members capacity
reserve splice
remove
remove_if
unique
merge
sort
reverse set
reset
flip
to_ulong
to_string
test
any
noneAmortized complexity shown. Legend: O(1) constant < O(log n) logarithmic < O(n) linear; *=depends on container
Container adaptors:
Container Adaptors | Headers<stack> | <queue> | Membersstack | queue | priority_queue | constructor*constructorconstructorconstructorcapacitysizeO(1)sizesizesizeemptyO(1)emptyemptyemptyelement accessfrontO(1) front backO(1) back topO(1)top topmodifierspushO(1)pushpushpushpopO(1)poppoppop