Containers library - cppreference.com

The Containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are two(until C++11)three(since C++11) classes of containers:

sequence containers,

associative containers,

unordered associative containers,

(since C++11)each of which is designed to support a different set of operations.

The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with properties similar to pointers).

Most containers have at least several member functions in common, and share functionalities. Which container is the best for the particular application depends not only on the offered functionality, but also on its efficiency for different workloads.

Sequence containers

Sequence containers implement data structures which can be accessed sequentially.

array

(C++11)

fixed-sized inplace contiguous array
(class template)

[edit]

vector

resizable contiguous array
(class template)

[edit]

inplace_vector

(C++26)

resizable, fixed capacity, inplace contiguous array
(class template)

[edit]

hive

(C++26)

collection that reuses erased elements' memory
(class template)

[edit]

deque

double-ended queue
(class template)

[edit]

forward_list

(C++11)

singly-linked list
(class template)

[edit]

list

doubly-linked list
(class template)

[edit]

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)

[edit]

collection of key-value pairs, sorted by keys, keys are unique
(class template)

[edit]

collection of keys, sorted by keys
(class template)

[edit]

collection of key-value pairs, sorted by keys
(class template)

[edit]

Unordered associative containers (since C++11)

Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) average, O(n) worst-case complexity).

Container adaptors

Container adaptors provide a different interface for sequential containers.

stack

adapts a container to provide stack (LIFO data structure)
(class template)

[edit]

queue

adapts a container to provide queue (FIFO data structure)
(class template)

[edit]

priority_queue

adapts a container to provide priority queue
(class template)

[edit]

flat_set

(C++23)

adapts a container to provide a collection of unique keys, sorted by keys
(class template)

[edit]

flat_map

(C++23)

adapts two containers to provide a collection of key-value pairs, sorted by unique keys
(class template)

[edit]

flat_multiset

(C++23)

adapts a container to provide a collection of keys, sorted by keys
(class template)

[edit]

flat_multimap

(C++23)

adapts two containers to provide a collection of key-value pairs, sorted by keys
(class template)

[edit]

Views (since C++20)

Views provide flexible facilities for interacting with one- or multi-dimensional views over a non-owning array of elements.

(C++20)

a non-owning view over a contiguous sequence of objects
(class template)

[edit]

(C++23)

a multi-dimensional non-owning array view
(class template)

[edit]

Iterator invalidation

Read-only methods never

invalidate

iterators or references. Methods which modify the contents of a container may invalidate iterators and/or references, as summarized in this table.

Category Container After insertion, are... After erasure, are... Conditionally iterators valid? references valid? iterators valid? references valid? Sequence containers

array

N/AN/A

vector

No N/AInsertion changed capacity Yes Yes Before modified element(s)
(for insertion only if capacity didn't change) No No At or after modified element(s)

deque

No Yes Yes, except erased element(s) Modified first or last element No No Modified middle only

list

Yes Yes, except erased element(s)

forward_list

Yes Yes, except erased element(s) Associative containers

set

multiset

map

multimap

Yes Yes, except erased element(s) Unordered associative containers

unordered_set

unordered_multiset

unordered_map

unordered_multimap

No Yes N/AInsertion caused rehash Yes Yes, except erased element(s) No rehash Here, insertion refers to any method which adds one or more elements to the container and erasure refers to any method which removes one or more elements from the container.

Examples of insertion methods are

std::set::insert

,

std::map::emplace

,

std::vector::push_back

, and

std::deque::push_front

.

Examples of erasure methods are

std::set::erase

,

std::vector::pop_back

,

std::deque::pop_front

, and

std::map::clear

. clear invalidates all iterators and references. Because it erases all elements, this technically complies with the rules above.

Unless otherwise specified (either explicitly or by defining a function in terms of other functions), passing a container as an argument to a library function never invalidate iterators to, or change the values of, objects within that container.

The past-the-end iterator deserves particular mention. In general this iterator is invalidated as though it were a normal iterator to a non-erased element. So

std::set::end

is never invalidated,

std::unordered_set::end

is invalidated only on rehash(since C++11),

std::vector::end

is always invalidated (since it is always after the modified elements), and so on.

There is one exception: an erasure which deletes the last element of a

std::deque

does invalidate the past-the-end iterator, even though it is not an erased element of the container (or an element at all). Combined with the general rules for

std::deque

iterators, the net result is that the only modifying operation which does not invalidate

std::deque::end

is an erasure which deletes the first element, but not the last.

Thread safety

All container functions can be called concurrently by different threads on different containers. More generally, the C++ standard library functions do not read objects accessible by other threads unless those objects are directly or indirectly accessible via the function arguments, including the this pointer.

All const member functions can be called concurrently by different threads on the same container. In addition, the member functions begin(), end(), rbegin(), rend(), front(), back(), data(), find(), lower_bound(), upper_bound(), equal_range(), at(), and, except in associative containers, operator[], behave as const for the purposes of thread safety (that is, they can also be called concurrently by different threads on the same container). More generally, the C++ standard library functions do not modify objects unless those objects are accessible, directly or indirectly, via the function's non-const arguments, including the this pointer.

Different elements in the same container can be modified concurrently by different threads, except for the elements of std::vector<bool> (for example, a vector of

std::future

objects can be receiving values from multiple threads).

Iterator operations (e.g. incrementing an iterator) read, but do not modify the underlying container, and may be executed concurrently with operations on other iterators on the same container, with the const member functions, or reads from the elements. Container operations that invalidate any iterators modify the container and cannot be executed concurrently with any operations on existing iterators even if those iterators are not invalidated.

Elements of the same container can be modified concurrently with those member functions that are not specified to access these elements. More generally, the C++ standard library functions do not read objects indirectly accessible through their arguments (including other elements of a container) except when required by its specification.

In any case, container operations (as well as algorithms, or any other C++ standard library functions) may be parallelized internally as long as this does not change the user-visible results (e.g.

std::transform

may be parallelized, but not

std::for_each

which is specified to visit each element of a sequence in order).

(since C++11)Function table

Note:

std::basic_string

is not treated as a container by the standard but behaves much like one due to its similarity. It is listed as 'Pseudo container' here for convenience.

- functions present in C++03 - functions present since C++11 - functions present since C++17 - functions present since C++20 - functions present since C++23 Member function table

Pseudo container Sequence containers Associative containers Unordered associative containers Container adaptors Header

<string>

<array>

<vector>

<deque>

<forward_list>

<list>

<set>

<map>

<unordered_set>

<unordered_map>

<stack>

<queue>

<flat_set>

<flat_map>

Header Container

basic_string

array

vector

deque

forward_list

list

set

multiset

map

multimap

unordered_set

unordered_multiset

unordered_map

unordered_multimap

stack

queue

priority_queue

flat_set

flat_multiset

flat_map

flat_multimap

Container (constructor)

basic_string

(implicit)

vector

deque

forward_list

list

set

multiset

map

multimap

unordered_set

unordered_multiset

unordered_map

unordered_multimap

stack

queue

priority_queue

flat_set

flat_multiset

flat_map

flat_multimap

(constructor)(destructor)

~basic_string

(implicit)

~vector

~deque

~forward_list

~list

~set

~multiset

~map

~multimap

~unordered_set

~unordered_multiset

~unordered_map

~unordered_multimap

~stack

~queue

~priority_queue

~flat_set

~flat_multiset

~flat_map

~flat_multimap

(destructor)operator=

operator=

(implicit)

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=

operator=assign

assign

assign

assign

assign

assign

assignassign_range

assign_range

assign_range

assign_range

assign_range

assign_range

assign_rangeIterators begincbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begin cbegin

begincbeginIterators endcend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

end cend

endcendrbegincrbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegin crbegin

rbegincrbeginrendcrend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rend crend

rendcrendElement
access at

at

at

at

at

at

at

at

atElement
access operator[]

operator[]

operator[]

operator[]

operator[]

operator[]

operator[]

operator[]

operator[]data

data

data

data

datafront

front

front

front

front

front

front

front

top

frontback

back

back

back

back

back

top

back

backCapacity empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

empty

emptyCapacity size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

size

sizemax_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_size

max_sizeresize

resize

resize

resize

resize

resize

resizecapacity

capacity

capacity

capacityreserve

reserve

reserve

reserve

reserve

reserve

reserve

reserveshrink_to_fit

shrink_to_fit

shrink_to_fit

shrink_to_fit

shrink_to_fitModifiers clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clear

clearModifiers insert

insert

insert

insert

insert_after

insert

insert

insert

insert

insert

insert

insert

insert

insert

insert

insert

insert

insert

insertinsert_range

insert_range

insert_range

insert_range

insert_range_after

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_range

insert_rangeinsert_or_assign

insert_or_assign

insert_or_assign

insert_or_assign

insert_or_assignemplace

emplace

emplace

emplace_after

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplace

emplaceemplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hint

emplace_hinttry_emplace

try_emplace

try_emplace

try_emplace

try_emplaceerase

erase

erase

erase

erase_after

erase

erase

erase

erase

erase

erase

erase

erase

erase

erase

erase

erase

erase

erasepush_front

push_front

push_front

push_front

push_frontprepend_range

prepend_range

prepend_range

prepend_range

prepend_rangeemplace_front

emplace_front

emplace_front

emplace_front

emplace_frontpop_front

pop_front

pop_front

pop_front

pop

pop

pop_frontpush_back

push_back

push_back

push_back

push_back

push

push

push

push_backappend_range

append_range

append_range

append_range

append_range

push_range

push_range

push_range

append_rangeemplace_back

emplace_back

emplace_back

emplace_back

emplace

emplace

emplace

emplace_backpop_back

pop_back

pop_back

pop_back

pop_back

pop

pop_backswap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swapmerge

merge

merge

merge

merge

merge

merge

merge

merge

merge

merge

mergeextract

[1]

extract

extract

extract

extract

extract

extract

extract

extract

extractList operations splice

splice_after

splice

spliceList operations remove

remove

remove

removeremove_if

remove_if

remove_if

remove_ifreverse

reverse

reverse

reverseunique

unique

unique

uniquesort

sort

sort

sortBucket and Hash begin(size_type)cbegin(size_type)

begin(size_type) cbegin(size_type)

begin(size_type) cbegin(size_type)

begin(size_type) cbegin(size_type)

begin(size_type) cbegin(size_type)

begin(size_type)cbegin(size_type)Bucket and Hash end(size_type)cend(size_type)

end(size_type) cend(size_type)

end(size_type) cend(size_type)

end(size_type) cend(size_type)

end(size_type) cend(size_type)

end(size_type)cend(size_type)bucket_count

bucket_count

bucket_count

bucket_count

bucket_count

bucket_countmax_bucket_count

max_bucket_count

max_bucket_count

max_bucket_count

max_bucket_count

max_bucket_countbucket_size

bucket_size

bucket_size

bucket_size

bucket_size

bucket_sizebucket

bucket

bucket

bucket

bucket

bucketload_factor

load_factor

load_factor

load_factor

load_factor

load_factormax_load_factor

max_load_factor

max_load_factor

max_load_factor

max_load_factor

max_load_factorrehash

rehash

rehash

rehash

rehash

rehashLookup count

count

count

count

count

count

count

count

count

count

count

count

count

countLookup find

find

find

find

find

find

find

find

find

find

find

find

find

find

findcontains

contains

contains

contains

contains

contains

contains

contains

contains

contains

contains

contains

contains

contains

containslower_bound

lower_bound

lower_bound

lower_bound

lower_bound

lower_bound

lower_bound

lower_bound

lower_bound

lower_boundupper_bound

upper_bound

upper_bound

upper_bound

upper_bound

upper_bound

upper_bound

upper_bound

upper_bound

upper_boundequal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_range

equal_rangeObservers key_comp

key_comp

key_comp

key_comp

key_comp

key_comp

key_comp

key_comp

key_comp

key_compObservers value_comp

value_comp

value_comp

value_comp

value_comp

value_comp

value_comp

value_comp

value_comp

value_comphash_function

hash_function

hash_function

hash_function

hash_function

hash_functionkey_eq

key_eq

key_eq

key_eq

key_eq

key_eqAllocator get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocator

get_allocatorAllocator Adaptors extract

[2]

extract

extract

extract

extract

extractAdaptors replace

replace

replace

replace

replace

replaceContainer

basic_string

array

vector

deque

forward_list

list

set

multiset

map

multimap

unordered_set

unordered_multiset

unordered_map

unordered_multimap

stack

queue

priority_queue

flat_set

flat_multiset

flat_map

flat_multimap

Container Header

<string>

<array>

<vector>

<deque>

<forward_list>

<list>

<set>

<map>

<unordered_set>

<unordered_map>

<stack>

<queue>

<flat_set>

<flat_map>

Header Pseudo container Sequence containers Associative containers Unordered associative containers Container adaptors Note: functions in two different extract lines have different meanings and syntax:

e.g., node_typeextract(const_iterator) or node_typeextract(Key&)

e.g., container_typeextract()&&

Non-member function table

Pseudo container Sequence containers Associative containers Unordered associative containers Container adaptors Header

<string>

<array>

<vector>

<deque>

<forward_list>

<list>

<set>

<map>

<unordered_set>

<unordered_map>

<stack>

<queue>

<flat_set>

<flat_map>

Header Container

basic_string

array

vector

deque

forward_list

list

set

multiset

map

multimap

unordered_set

unordered_multiset

unordered_map

unordered_multimap

stack

queue

priority_queue

flat_set

flat_multiset

flat_map

flat_multimap

Container Non-member function operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==

operator==Non-member function operator!= (removed in C++20)

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!=

operator!= (removed in C++20)operator< (removed in C++20)

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator<

operator< (removed in C++20)operator<= (removed in C++20)

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<=

operator<= (removed in C++20)operator> (removed in C++20)

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator>

operator> (removed in C++20)operator>= (removed in C++20)

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>=

operator>= (removed in C++20)operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>

operator<=>swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swap

swaperase

erase

erase

erase

erase

erase

eraseerase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_if

erase_ifContainer

basic_string

array

vector

deque

forward_list

list

set

multiset

map

multimap

unordered_set

unordered_multiset

unordered_map

unordered_multimap

stack

queue

priority_queue

flat_set

flat_multiset

flat_map

flat_multimap

Container Header

<string>

<array>

<vector>

<deque>

<forward_list>

<list>

<set>

<map>

<unordered_set>

<unordered_map>

<stack>

<queue>

<flat_set>

<flat_map>

Header Pseudo container Sequence containers Associative containers Unordered associative containers Container adaptors The <, <=, >, >=, and != operators are

synthesized

from operator<=> and operator== respectively.

(since C++20)Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 51

C++98 container iterators might be invalidated
by arbitrary library operation they are only invalidated
when specified See also

C++ named requirements:

Container

SequenceContainer

ContiguousContainer

ReversibleContainer

AssociativeContainer

AllocatorAwareContainer

UnorderedAssociativeContainer