parent nodes: STLapuntes

C++ Standard Template Library

Containers

Container Class Name Container Type Insertion Deletion Lookup When to Use
vector Sequential O(1) at end
O(N) anywhere else
O(1) at end
O(N) anywhere else
O(1) * Need quick lookup
* Don’t mind slow insertion/deletion
* Whenever you would use an array
list Sequential O(1) O(1) O(N) * Need quick insertion/deletion
* Don’t mind slow lookup
deque Sequential O(1) at beginning or end
O(N) anywhere else
O(1) at beginning or end
O(N) anywhere else
O(1) * Not usually needed
* use a vector or a list instead
queue Container Adapter O(1) O(1) N/A * When you want a FIFO structure
priority_queue Container Adapter O(log(N)) O(log(N)) N/A * When you want a FIFO structure with priority
stack Container Adapter O(1) O(1) N/A * When you want a FILO structure
set / multiset Sorted Associative O(log(N)) O(log(N)) O(log(N)) * When you want a collection of elements with equal lookup, insertion, and deletion times
map / multimap Sorted Associative O(log(N)) O(log(N)) O(log(N)) * When you want to associate keys and values
bitset Special N/A N/A O(1) * When you want a collection of flags

Nonmodifying Algorithms

The nonmodifying algorithms are those that look at a sequence of elements and return some information
about the elements, or execute some function on each element. As “nonmodifying” algorithms, they cannot
change the values of elements or the order of elements within the sequence. This category contains
four types of algorithms. The following tables list and provide brief summaries of the various nonmodifying
algorithms. With these algorithms, you should rarely need to write a for loop to iterate over a
sequence of values.

Search Algorithms

Algorithm Name Algorithm Synopsis Requires Sorted
Sequence?
find(), find_if() Finds the first element that matches a value
or causes a predicate to return true
No
find_first_of() Like find, except searches for one of several
elements at the same time
No
adjacent_find() Finds the first instance of two consecutive
elements that are equal to each other
No
search(), find_end()Finds the first (search()) or last (find_end())
subsequence in a sequence that matches another sequence
No
search_n() Finds the first instance of n consecutive
elements that are equal to a given value
No
lower_bound()
upper_bound()
equal_range()
Finds the beginning (lower_bound()) end (upper_bound()), or
both sides (equal_range()) of the range including a specified
element
Yes
binary_search() Finds a value in a sorted sequence Yes
min_element()
max_element()
Finds the minimum or maximum element in a sequence No

Numerical Processing Algorithms

Algorithm Name Algorithm Synopsis Requires Sorted
Sequence?

Ejemplos

VectorEjemplo