Containers

Sequencial Containers

Elems ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

Vector

< vector>

representing arrays that can change in size

  • contiguous storage
  • access using offsets
  • storage handled automatically

Compare to other seq.cont.

  • efficient access
  • relatively efficient adding or removing (end)
  • inserting or removing (other pos.) - worse

Note

vector may allocate extra storage to accommodate for possible growth.

it may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it.

  • amortized constant time complexity

Array

< array>

  • fixed-size
  • contiguous storage
  • access using offsets
  • does not keep any data other than the elem
  • can be treated as tuple objects

Deque (Double ended queue)

< deque>

  • dynamic size
  • not guaranteed to store all its elements in contiguous storage
  • iterators

Note

access by offset can cause undefined behavious

  • efficient insertion and deletion (not only at its end)
  • worse insertion and deletion at other pos.

Forward List

< forward_list>

  • impl. singly-linked lists
  • constant time insert and erase (anywhere)
  • more efficient than list

Compare to other cont.

  • better in inserting, extracting and moving elem(anywhere)
  • lack direct access

List

< list>

  • impl. doubly-linked lists
  • constant time insert and erase(anywhere)

Compare to other cont.

  • better in inserting, extracting and moving elem(anywhere)
  • lack direct access

Container Adaptors

classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements.

Support operations:

  • empty()
  • size()
  • front()
  • push_back()
  • pop_back()

Queue

< queue>

designed to operate in a FIFO

Underlying Container

deque | list

Priority Queue

< queue>

designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.

  • random access iters.

Underlying Container

vector | deque

Stack

< stack>

designed to operate in a LIFO

  • inserted/extracted only from one end

Underlying Container

deque | vector | list

Associative Containers

store elements formed by a combination of a key value and a mapped value, following a specific order

Map

< map>

key values are generally used to sort and uniquely identify the elem

grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;

  • impl. as binary search trees
  • unique keys
  • sorted
  • slower than unordered_map
  • direct access by key using []

Mutlimap

< map>

Note

Same as MAP but elements in the container can have equivalent keys

Unordered Map

< unordered_map>

  • not sorted
  • unique keys
  • organized into buckets depending on their hash values
  • faster than map
  • less efficient for range iteration through a subset of their elem

Unordered Mutlimap

< unordered_map>

Elements with equivalent keys are grouped together in the same bucket and in such a way that an iterator (see equal_range) can iterate through all of them.

Set

< set>

store unique elements following a specific order

Note

the value is itself the key, of type T

  • each value is unique
  • value cannot be modified once inside
  • sorted
  • slower than unordered_set

Multiset

< set>

Note

the value is itself the key, of type T

  • value cannot be modified once inside
  • sorted
  • impl. as binary search trees
  • can have equivalent keys
  • slower than unordered_multiset

Sequence containers

Headers< array>< vector>< deque><forward_list>< list>
Membersarrayvectordequeforward_listlist
constructorimplicitvectordequeforward_listlist
destructorimplicit~vector~deque~forward_list~list
operator=implicitoperator=operator=operator=operator=
beginbeginbeginbeginbegin/before_beginbegin
endendendendendend
rbeginrbeginrbeginrbeginrbegin
rendrendrendrendrend
cbegincbegincbegincbegincbegin/cbefore_begincbegin
cendcendcendcendcendcend
crbegincrbegincrbegincrbegincrbegin
crendcrendcrendcrendcrend
capacitysizesizesizesize
max_sizemax_sizemax_sizemax_sizemax_sizemax_size
emptyemptyemptyemptyemptyempty
resizeresizeresizeresizeresize
shrink_to_fitshrink_to_fitshrink_to_fit
capacitycapacity
reservereserve
element accessfrontfrontfrontfrontfront
backbackbackbackback
operator[]operator[]operator[]operator[]
atatatat
assignassignassignassign
emplaceemplaceemplaceemplace_after
insertinsertinsertinsert_after
eraseerase eraseerase_aftererase
emplace_backemplace_backemplace_backemplace_back
push_backpush_backpush_backpush_back
pop_backpop_backpop_backpop_back
emplace_frontemplace_frontemplace_frontemplace_front
push_frontpush_frontpush_frontpush_front
pop_frontpop_frontpop_frontpop_front
clearclearclearclearclear
swapswapswapswapswapswap
splicesplice_aftersplice
removeremoveremove
remove_ifremove_ifremove_if
uniqueuniqueunique
mergemergemerge
sortsortsort
reversereversereverse
get_allocatorget_allocatorget_allocatorget_allocatorget_allocator
datadatadata

Associative containers

Headers< set>< map><unordered_set><unordered_map>
Memberssetmultisetmapmultimapunordered_setunordered_multisetunordered_mapunordered_multimap
constructorsetmultisetmap multimapunordered_setunordered_multisetunordered_mapunordered_multimap
destructor~set~multiset~map~multimap~unordered_set~unordered_multiset~unordered_map~unordered_multimap
assignmentoperator=operator=operator=operator=operator=operator=operator=operator=
beginbeginbeginbeginbeginbeginbeginbeginbegin
endendendendendendendendend
rbeginrbeginrbeginrbeginrbegin
rendrendrendrendrend
cbegincbegincbegincbegincbegincbegincbegincbegincbegin
cendcendcendcendcendcendcendcendcend
crbegincrbegincrbegincrbegincrbegin
crendcrendcrendcrendcrend
capacitysizesizesizesizesizesizesizesize
max_sizemax_sizemax_sizemax_sizemax_sizemax_sizemax_sizemax_sizemax_size
emptyemptyemptyemptyemptyemptyemptyemptyempty
reservereservereservereservereserve
atatat
operator[]operator[]operator[]
emplaceemplaceemplaceemplaceemplaceemplaceemplaceemplaceemplace
emplace_hintemplace_hintemplace_hintemplace_hintemplace_hintemplace_hintemplace_hintemplace_hintemplace_hint
insertinsertinsertinsertinsertinsertinsertinsertinsert
eraseeraseeraseeraseeraseeraseeraseeraseerase
clearclearclearclearclearclearclearclearclear
swapswapswapswapswapswapswapswapswap
countcountcountcountcountcountcountcountcount
findfindfindfindfindfindfindfindfind
equal_rangeequal_rangeequal_rangeequal_rangeequal_rangeequal_rangeequal_rangeequal_rangeequal_range
lower_boundlower_boundlower_boundlower_boundlower_bound
upper_boundupper_boundupper_boundupper_boundupper_bound
get_allocatorget_allocatorget_allocatorget_allocatorget_allocatorget_allocatorget_allocatorget_allocatorget_allocator
key_compkey_compkey_compkey_compkey_comp
value_compvalue_compvalue_compvalue_compvalue_comp
key_eqkey_eqkey_eqkey_eqkey_eq
hash_functionhash_functionhash_functionhash_functionhash_function
buckets bucketbucketbucketbucketbucket
bucket_countbucket_countbucket_countbucket_countbucket_count
bucket_sizebucket_sizebucket_sizebucket_sizebucket_size
max_bucket_countmax_bucket_countmax_bucket_countmax_bucket_countmax_bucket_count
rehashrehashrehashrehashrehash
load_factorload_factorload_factorload_factorload_factor
max_load_factormax_load_factormax_load_factormax_load_factormax_load_factor
Last Updated: