Set 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

Iterators
rbegin
crbegin( )

A

returns a reverse iterator to the beginning
Parameters
(none)

Return value
Reverse iterator to the first element.

Complexity
Constant.

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

Iterators
rend
crend( )

A

returns a reverse iterator to the end
Parameters
(none)

Return value
Reverse 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
5
Q

Capacity

empty( )

A

checks whether the container is empty
Parameters
(none)

Return value
true if the container is empty, false otherwise

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
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
7
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
8
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
9
Q

Modifiers

insert( )

A

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-2) Returns a pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool value set to true if 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-8
Complexity
1-2) Logarithmic in the size of the container, O(log(size())).
3-4) Amortized constant if the insertion happens in the position just after the hint, logarithmic in the size of the container otherwise.
(until C++11)
3-4) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.
(since C++11)
5-6) O(N*log(size() + N)), where N is the number of elements to insert.
7) Logarithmic in the size of the container, O(log(size())).
8) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.

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

Modifiers

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
Logarithmic 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 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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 set:

1) Amortized constant
2) log(c.size()) + std::distance(first, last)
3) log(c.size()) + c.count(key)

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 Compare objects.

(until C++17)
noexcept specification:  
noexcept(std::allocator_traits::is_always_equal::value
&& std::is_nothrow_swappable::value)
(since C++17)
Complexity
Constant.
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) amortized constant
2) log(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)

Exceptions
Does not throw unless comparison throws.

Complexity
N*log(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

count( )

A

returns the number of elements matching specific key
Parameters
key - key value of the elements to count
x - alternative value to compare to the keys
Return value
Number of elements with key that compares equivalent to key or x, which is either 1 or 0 for (1).

Complexity
Logarithmic in the size of the container.

17
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
Logarithmic in the size of the container.

18
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
Logarithmic in the size of the container.

19
Q
Lookup
equal_range( )
A

returns range of elements matching a specific key
Parameters
key - key value to compare the elements to
x - alternative value that can be compared to Key
Return value
std::pair containing a pair of iterators defining the wanted range: the first pointing to the first element that is not less than key and the second pointing to the first element greater than key.

If there are no elements not less than key, past-the-end (see end()) iterator is returned as the first element. Similarly if there are no elements greater than key, past-the-end iterator is returned as the second element.

Complexity
Logarithmic in the size of the container.

20
Q

Lookup

lower_bound( )

A

returns an iterator to the first element not less than the given key
Parameters
key - key value to compare the elements to
x - alternative value that can be compared to Key
Return value
Iterator pointing to the first element that is not less than key. If no such element is found, a past-the-end iterator (see end()) is returned.

Complexity
Logarithmic in the size of the container.

21
Q

Lookup

upper_bound( )

A

returns an iterator to the first element greater than the given key
Parameters
key - key value to compare the elements to
x - alternative value that can be compared to Key
Return value
Iterator pointing to the first element that is greater than key. If no such element is found, past-the-end (see end()) iterator is returned.

Complexity
Logarithmic in the size of the container.

22
Q

Observers

key_comp( )

A

returns the function that compares keys
Parameters
(none)

Return value
The key comparison function object.

Complexity
Constant.

23
Q

Observers

value_comp( )

A

returns the function that compares keys in objects of type value_type
Parameters
(none)

Return value
The value comparison function object.

Complexity
Constant.