Uunordered_map Flashcards

1
Q

Iterators
begin
cbegin( )

A

returns an iterator to the beginning
Parameters
(none)

Return value
Iterator to the first element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Iterators
end
cend( )

A

returns an iterator to the end
Parameters
(none)

Return value
Iterator to the element following the last element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Capacity

empty( )

A

checks whether the container is empty
Parameters
(none)

Return value
true if the container is empty, false otherwise

Complexity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Capacity

size( )

A

returns the number of elements
Parameters
(none)

Return value
The number of elements in the container.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Capacity

max_size( )

A

returns the maximum possible number of elements
Parameters
(none)

Return value
Maximum number of elements.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Modifiers

erase( )

A

erases elements
Parameters
pos - iterator to the element to remove
first, last - range of elements to remove
key - key value of the elements to remove
Return value
1-2) Iterator following the last removed element.
3) Number of elements removed (0 or 1).
Exceptions
1,2) Throws nothing.
3) Any exceptions thrown by the Compare object.
Complexity
Given an instance c of unordered_map:

1) Average case: constant, worst case: c.size()
2) Average case: std::distance(first, last), worst case: c.size()
3) Average case: c.count(key), worst case: c.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Modifiers

clear( )

A

clears the contents
Parameters
(none)

Return value
(none)

Complexity
Linear in the size of the container, i.e., the number of elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Modifiers

insert( )

A

inserts elements
Parameters
hint - iterator, used as a suggestion as to where to insert the content
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-2) 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.
3-4) Returns an iterator to the inserted element, or to the element that prevented the insertion.
5-6) (none)
7) 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().
8) 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-4) If an exception is thrown by any operation, the insertion has no effect.
This section is incomplete
Reason: cases 5-6
Complexity
1-4) Average case: O(1), worst case O(size())
5-6) Average case: O(N), where N is the number of elements to insert. Worst case: O(N*size()+N)
7-8) Average case: O(1), worst case O(size())
Notes
The hinted insert (3,4) does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such as std::vector::insert. This makes it possible to create generic inserters such as std::inserter. One way to check success of a hinted insert is to compare size() before and after.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Modifiers

insert_or_assign( )

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Modifi

emplace( )

A

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
Amortized constant on average, worst case linear in the size of the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Modifiers
emplace_hint( )
A

constructs elements in-place using a hint
Parameters
hint - iterator, used as a suggestion as to where to insert the new element
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
Amortized constant on average, worst case linear in the size of the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Modifiers
try_emplace( )
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Modifiers

swap( )

A
swaps the contents
Parameters
other	-	container to exchange the contents with
Return value
(none)

Exceptions
Any exception thrown by the swap of the Hash or KeyEqual objects.

(until C++17)
noexcept specification:  
noexcept(std::allocator_traits::is_always_equal::value
&& std::is_nothrow_swappable::value
&& std::is_nothrow_swappable::value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Modifiers

extract( )

A

extracts nodes from the container
Parameters
position - a valid iterator into this container
x - a key to identify the node to be extracted
Return value
A node handle that owns the extracted element, or empty node handle in case the element is not found in overload (2)

Complexity
1,2) Average case O(1), worst case O(a.size()).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Modifiers

merge( )

A
splices nodes from another container
Parameters
source	-	compatible container to transfer the nodes from
Return value
(none)

Complexity
Average case O(N), worst case O(N*size()+N), where N is source.size().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Lookup

at( )

A

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
Average case: constant, worst case: linear in size.

17
Q

Lookup

operator[]

A
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
Average case: constant, worst case: linear in size.

18
Q

Lookup

count( )

A

returns the number of elements matching specific key
Parameters
key - key value of the elements to count
x - a value of any type that can be transparently compared with a key
Return value
1) Number of elements with key key, that is either 1 or 0.
2) Number of elements with key that compares equivalent to x.
Complexity
Constant on average, worst case linear in the size of the container.

19
Q

Lookup

find( )

A

finds element with specific key
Parameters
key - key value of the element to search for
x - a value of any type that can be transparently compared with a key
Return value
Iterator to an element with key equivalent to key. If no such element is found, past-the-end (see end()) iterator is returned.

Complexity
Constant on average, worst case linear in the size of the container.

20
Q

Lookup

contains( )

A

checks if the container contains element with specific key
Parameters
key - key value of the element to search for
x - a value of any type that can be transparently compared with a key
Return value
true if there is such an element, otherwise false.

Complexity
Constant on average, worst case linear in the size of the container.

21
Q
Lookup
equal_range( )
A

returns range of elements matching a specific key
Parameters
key - key value to compare the elements to
x - a value of any type that can be transparently compared with a key
Return value
std::pair containing a pair of iterators defining the wanted range. If there are no such elements, past-the-end (see end()) iterators are returned as both elements of the pair.

Complexity
Average case linear in the number of elements with the key key, worst case linear in the size of the container.

22
Q

Bucket interface
begin(size_type)
cbegin(size_type)

A
returns an iterator to the beginning of the specified bucket
Parameters
n	-	the index of the bucket to access
Return value
Iterator to the first element.

Complexity
Constant.

23
Q

Bucket interface
end(size_type)
cend(size_type)

A

returns an iterator to the end of the specified bucket
Parameters
n - the index of the bucket to access
Return value
iterator to the element following the last element

Complexity
Constant

24
Q

Bucket interface

bucket_count( )

A

returns the number of buckets
Parameters
(none)

Return value
The number of buckets in the container.

Complexity
Constant.

25
Q

Bucket interface

max_bucket_count( )

A

returns the maximum number of buckets
Parameters
(none)

Return value
Maximum number of buckets.

Complexity
Constant.

26
Q

Bucket interface

bucket_size( )

A
returns the number of elements in specific bucket
Parameters
n	-	the index of the bucket to examine
Return value
The number of elements in the bucket n.

Complexity
Linear in the size of the bucket n.

27
Q

Bucket interface

bucket( )

A
returns the bucket for specific key
Parameters
key	-	the value of the key to examine
Return value
Bucket index for the key key.

Complexity
Constant.

28
Q

Hash policy

load_factor( )

A

returns average number of elements per bucket
Parameters
(none)

Return value
Average number of elements per bucket.

Complexity
Constant.

29
Q

Hash policy

max_load_factor( )

A
manages maximum average number of elements per bucket
Parameters
ml	-	new maximum load factor setting
Return value
1) current maximum load factor.

2) none.

Complexity
Constant

30
Q

Hash policy

rehash( )

A
reserves at least the specified number of buckets and regenerates the hash table
Parameters
count	-	new number of buckets
Return value
(none)

Complexity
Average case linear in the size of the container, worst case quadratic.

31
Q

Hash policy

reserve( )

A
reserves space for at least the specified number of elements and regenerates the hash table
Parameters
count	-	new capacity of the container
Return value
(none)

Complexity
Average case linear in the size of the container, worst case quadratic.

32
Q
Observers
hash_function( )
A

returns function used to hash the keys
Parameters
(none)

Return value
The hash function.

Complexity
Constant.

33
Q

Observers

key_eq( )

A

returns the function used to compare keys for equality
Parameters
(none)

Return value
The key comparison function.

Complexity
Constant.