cplusplus
.com
教程
參考
文章
論壇
C++
教程
參考
文章
論壇
參考
C 庫
<cassert> (assert.h)
<cctype> (ctype.h)
<cerrno> (errno.h)
C++11
<cfenv> (fenv.h)
<cfloat> (float.h)
C++11
<cinttypes> (inttypes.h)
<ciso646> (iso646.h)
<climits> (limits.h)
<clocale> (locale.h)
<cmath> (math.h)
<csetjmp> (setjmp.h)
<csignal> (signal.h)
<cstdarg> (stdarg.h)
C++11
<cstdbool> (stdbool.h)
<cstddef> (stddef.h)
C++11
<cstdint> (stdint.h)
<cstdio> (stdio.h)
<cstdlib> (stdlib.h)
<cstring> (string.h)
C++11
<ctgmath> (tgmath.h)
<ctime> (time.h)
C++11
<cuchar> (uchar.h)
<cwchar> (wchar.h)
<cwctype> (wctype.h)
容器
C++11
<array>
<deque>
C++11
<forward_list>
<list>
<map>
<queue>
<set>
<stack>
C++11
<unordered_map>
C++11
<unordered_set>
<vector>
輸入/輸出
<fstream>
<iomanip>
<ios>
<iosfwd>
<iostream>
<istream>
<ostream>
<sstream>
<streambuf>
多執行緒
C++11
<atomic>
C++11
<condition_variable>
C++11
<future>
C++11
<mutex>
C++11
<thread>
其他
<algorithm>
<bitset>
C++11
<chrono>
C++11
<codecvt>
<complex>
<exception>
<functional>
C++11
<initializer_list>
<iterator>
<limits>
<locale>
<memory>
<new>
<numeric>
C++11
<random>
C++11
<ratio>
C++11
<regex>
<stdexcept>
<string>
C++11
<system_error>
C++11
<tuple>
C++11
<type_traits>
C++11
<typeindex>
<typeinfo>
<utility>
<valarray>
參考
容器
庫
容器
標準容器
容器是一種持有者物件,用於儲存其他物件的集合(其元素)。它們以類模板的形式實現,這使得所支援的元素型別具有很大的靈活性。
容器管理其元素的儲存空間,並提供成員函式來直接或透過迭代器(具有類似指標屬性的引用物件)訪問它們。
容器複製了程式設計中非常常用的結構:動態陣列 (
vector
)、佇列 (
queue
)、棧 (
stack
)、堆 (
priority_queue
)、連結串列 (
list
)、樹 (
set
)、關聯陣列 (
map
)...
許多容器有幾個共同的成員函式,並共享功能。針對特定需求決定使用哪種型別的容器,通常不僅取決於容器提供的功能,還取決於其某些成員的效率(複雜性)。對於序列容器尤其如此,它們在插入/刪除元素和訪問元素之間的複雜性方面提供了不同的權衡。
stack
、
queue
和
priority_queue
被實現為
容器介面卡
。容器介面卡不是完整的容器類,而是提供特定介面的類,它依賴於某個容器類(如
deque
或
list
)的物件來處理元素。底層容器被封裝起來,使得其元素可以被
容器介面卡
的成員訪問,而與所使用的底層
容器
類無關。
容器類模板
序列容器
:
array
陣列類
(類模板)
vector
向量
(類模板)
deque
雙端佇列
(類模板)
forward_list
前向列表
(類模板)
list
列表
(類模板)
容器介面卡
:
stack
後進先出棧
(類模板)
queue
先進先出佇列
(類模板)
priority_queue
優先佇列
(類模板)
關聯容器
:
set
集合
(類模板)
multiset
多鍵集合
(類模板)
map
對映
(類模板)
multimap
多鍵對映
(類模板)
無序關聯容器
:
unordered_set
無序集合
(類模板)
unordered_multiset
無序多重集合
(類模板)
unordered_map
無序對映
(類模板)
unordered_multimap
無序多重對映
(類模板)
其他
:
有兩個類模板與容器共享某些屬性,有時會與它們一起分類:
bitset
和
valarray
。
成員一覽表
這是一個比較圖表,列出了不同容器中存在的不同成員函式
圖例
:
C++98
C++98 起可用
C++11
C++11 新增
序列容器
標頭檔案
<array>
<vector>
<deque>
<forward_list>
<list>
成員
array
vector
deque
forward_list
list
建構函式
隱式
vector
deque
forward_list
list
解構函式
隱式
~vector
~deque
~forward_list
~list
operator=
隱式
operator=
operator=
operator=
operator=
迭代器
begin
begin
begin
begin
begin
before_begin
begin
end
end
end
end
end
end
rbegin
rbegin
rbegin
rbegin
rbegin
rend
rend
rend
rend
rend
常量迭代器
cbegin
cbegin
cbegin
cbegin
cbegin
cbefore_begin
cbegin
cend
cend
cend
cend
cend
cend
crbegin
crbegin
crbegin
crbegin
crbegin
crend
crend
crend
crend
crend
容量
size
size
size
size
size
max_size
max_size
max_size
max_size
max_size
max_size
empty
empty
empty
empty
empty
empty
resize
resize
resize
resize
resize
shrink_to_fit
shrink_to_fit
shrink_to_fit
容量
容量
reserve
reserve
元素訪問
front
front
front
front
front
front
back
back
back
back
back
operator[]
operator[]
operator[]
operator[]
at
at
at
at
修改器
assign
assign
assign
assign
assign
emplace
emplace
emplace
emplace_after
emplace
insert
insert
insert
insert_after
insert
erase
erase
erase
erase_after
erase
emplace_back
emplace_back
emplace_back
emplace_back
push_back
push_back
push_back
push_back
pop_back
pop_back
pop_back
pop_back
emplace_front
emplace_front
emplace_front
emplace_front
push_front
push_front
push_front
push_front
pop_front
pop_front
pop_front
pop_front
clear
clear
clear
clear
clear
swap
swap
swap
swap
swap
swap
列表操作
splice
splice_after
splice
remove
remove
remove
remove_if
remove_if
remove_if
unique
unique
unique
merge
merge
merge
sort
sort
sort
reverse
reverse
reverse
觀察器
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
data
data
data
關聯容器
標頭檔案
<set>
<map>
<unordered_set>
<unordered_map>
成員
set
multiset
map
multimap
unordered_set
unordered_multiset
unordered_map
unordered_multimap
建構函式
set
multiset
map
multimap
unordered_set
unordered_multiset
unordered_map
unordered_multimap
解構函式
~set
~multiset
~map
~multimap
~unordered_set
~unordered_multiset
~unordered_map
~unordered_multimap
賦值
operator=
operator=
operator=
operator=
operator=
operator=
operator=
operator=
迭代器
begin
begin
begin
begin
begin
begin
begin
begin
begin
end
end
end
end
end
end
end
end
end
rbegin
rbegin
rbegin
rbegin
rbegin
rend
rend
rend
rend
rend
常量迭代器
cbegin
cbegin
cbegin
cbegin
cbegin
cbegin
cbegin
cbegin
cbegin
cend
cend
cend
cend
cend
cend
cend
cend
cend
crbegin
crbegin
crbegin
crbegin
crbegin
crend
crend
crend
crend
crend
容量
size
size
size
size
size
size
size
size
size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
max_size
empty
empty
empty
empty
empty
empty
empty
empty
empty
reserve
reserve
reserve
reserve
reserve
元素訪問
at
at
at
operator[]
operator[]
operator[]
修改器
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
emplace_hint
insert
insert
insert
insert
insert
insert
insert
insert
insert
erase
erase
erase
erase
erase
erase
erase
erase
erase
clear
clear
clear
clear
clear
clear
clear
clear
clear
swap
swap
swap
swap
swap
swap
swap
swap
swap
操作
count
count
count
count
count
count
count
count
count
find
find
find
find
find
find
find
find
find
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
equal_range
lower_bound
lower_bound
lower_bound
lower_bound
lower_bound
upper_bound
upper_bound
upper_bound
upper_bound
upper_bound
觀察器
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
get_allocator
key_comp
key_comp
key_comp
key_comp
key_comp
value_comp
value_comp
value_comp
value_comp
value_comp
key_eq
key_eq
key_eq
key_eq
key_eq
hash_function
hash_function
hash_function
hash_function
hash_function
桶
bucket
bucket
bucket
bucket
bucket
bucket_count
bucket_count
bucket_count
bucket_count
bucket_count
bucket_size
bucket_size
bucket_size
bucket_size
bucket_size
max_bucket_count
max_bucket_count
max_bucket_count
max_bucket_count
max_bucket_count
雜湊策略
rehash
rehash
rehash
rehash
rehash
load_factor
load_factor
load_factor
load_factor
load_factor
max_load_factor
max_load_factor
max_load_factor
max_load_factor
max_load_factor