Map Flashcards
Element access
at( )
access specified element with bounds checking
Parameters
key - the key of the element to find
Return value
Reference to the mapped value of the requested element
Exceptions
std::out_of_range if the container does not have an element with the specified key
Complexity
Logarithmic in the size of the container.
Element access
operator[]
access or insert specified element Parameters key - the key of the element to find Return value Reference to the mapped value of the new element if no element with key key existed. Otherwise a reference to the mapped value of the existing element whose key is equivalent to key.
Exceptions
If an exception is thrown by any operation, the insertion has no effect
Complexity
Logarithmic in the size of the container.
Iterators
begin
cbegin( )
returns an iterator to the beginning
Parameters
(none)
Return value
Iterator to the first element.
Complexity
Constant.
Iterators
end
cend( )
returns an iterator to the end
Parameters
(none)
Return value
Iterator to the element following the last element.
Complexity
Constant.
Iterators
rbegin
crbegin( )
returns a reverse iterator to the beginning
Parameters
(none)
Return value
Reverse iterator to the first element.
Complexity
Constant.
Iterators
rend
crend( )
returns a reverse iterator to the end
Parameters
(none)
Return value
Reverse iterator to the element following the last element.
Complexity
Constant.
Capacity
empty( )
checks whether the container is empty
Parameters
(none)
Return value
true if the container is empty, false otherwise
Complexity
Constant.
Capacity
size( )
returns the number of elements
Parameters
(none)
Return value
The number of elements in the container.
Complexity
Constant.
Capacity
max_size( )
returns the maximum possible number of elements
Parameters
(none)
Return value
Maximum number of elements.
Complexity
Constant.
Modifiers
clear( )
clears the contents
Parameters
(none)
Return value (none)
Complexity
Linear in the size of the container, i.e., the number of elements.
Modifiers
insert( )
inserts elements or nodes
Parameters
hint -
iterator, used as a suggestion as to where to start the search (until C++11)
iterator to the position before which the new element will be inserted (since C++11)
value - element value to insert
first, last - range of elements to insert
ilist - initializer list to insert the values from
nh - a compatible node handle
Type requirements
-InputIt must meet the requirements of LegacyInputIterator.
Return value
1-3) Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
4-6) Returns an iterator to the inserted element, or to the element that prevented the insertion.
7-8) (none)
9) Returns an insert_return_type with the members initialized as follows: if nh is empty, inserted is false, position is end(), and node is empty. Otherwise if the insertion took place, inserted is true, position points to the inserted element, and node is empty. If the insertion failed, inserted is false, node has the previous value of nh, and position points to an element with a key equivalent to nh.key().
10) End iterator if nh was empty, iterator pointing to the inserted element if insertion took place, and iterator pointing to an element with a key equivalent to nh.key() if it failed.
Exceptions
1-6) If an exception is thrown by any operation, the insertion has no effect.
This section is incomplete
Reason: cases 7-10
Complexity
1-3) Logarithmic in the size of the container, O(log(size())).
4-6) Amortized constant if the insertion happens in the position just after the hint, logarithmic in the size of the container otherwise.
(until C++11)
4-6) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.
(since C++11)
7-8) O(N*log(size() + N)), where N is the number of elements to insert.
9) Logarithmic in the size of the container, O(log(size())).
10) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.
Modifiers
insert_or_assign( )
inserts an element or assigns to the current element if the key already exists
Parameters
k - the key used both to look up and to insert if not found
hint - iterator to the position before which the new element will be inserted
obj - the value to insert or assign
Return value
1,2) The bool component is true if the insertion took place and false if the assignment took place. The iterator component is pointing at the element that was inserted or updated
3,4) Iterator pointing at the element that was inserted or updated
Complexity
1,2) Same as for emplace
3,4) Same as for emplace_hint
Modifiers
emplace( )
constructs element in-place
Parameters
args - arguments to forward to the constructor of the element
Return value
Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place (true if insertion happened, false if it did not).
Exceptions
If an exception is thrown by any operation, this function has no effect.
Complexity
Logarithmic in the size of the container.
Modifiers emplace_hint( )
constructs elements in-place using a hint
Parameters
hint - iterator to the position before which the new element will be inserted
args - arguments to forward to the constructor of the element
Return value
Returns an iterator to the newly inserted element.
If the insertion failed because the element already exists, returns an iterator to the already existing element with the equivalent key.
Exceptions
If an exception is thrown by any operation, this function has no effect (strong exception guarantee).
Complexity
Logarithmic in the size of the container in general, but amortized constant if the new element is inserted just before hint.
Modifiers try_emplace( )
inserts in-place if the key does not exist, does nothing if the key exists
Parameters
k - the key used both to look up and to insert if not found
hint - iterator to the position before which the new element will be inserted
args - arguments to forward to the constructor of the element
Return value
1,2) Same as for emplace
3,4) Same as for emplace_hint
Complexity
1,2) Same as for emplace
3,4) Same as for emplace_hint