Advanced C++ STL: Tony Wong 2017-03-25
Advanced C++ STL: Tony Wong 2017-03-25
Tony Wong
2017-03-25
C++ Standard Template Library
▸ Algorithms
▹ Sorting
▹ Searching...
▸ Data structures
▹ Dynamically-sized array
▹ Queues...
▸ The implementation is abstracted away from the users
▹ The implementation may differ from compilers, and may change over
time (to improve performances)
Advanced C++ STL 2
What is a template?
▸ Class / function template can be applied to different data types
Result: 1 2 3 4 4 5 6 7 8 9
Input: 1 4 2 8 5 7
Output: 1 2 4 5 7 8 Advanced C++ STL 8
<algorithm> sort
▸ To sort user-defined types, you need to "teach" sort how to
compare objects:
▹ Implement operator<
bool operator<(const T& o) const
Method 1: Method 2:
▸ stable_sort(RndAccIt l, RndAccIt r)
▸ It only has effect when some data is not part of the sorting key,
e.g. string name
▸ For details, read Sorting and Searching
1.38s 3.08s
Advanced C++ STL 12
<algorithm> nth_element Average case: 𝑂(𝑛)
Worst case: 𝑂(𝑛 lg 𝑛)
Possible output:
5314247689
Advanced C++ STL 13
<algorithm> reverse 𝑂(𝑛)
▸ reverse(BiDirIt l, BiDirIt r)
▸ Obviously, reverses the range [l, r)
Result: abcdcba
▸ To get the distinct elements in a range, sort the range first then
apply unique
Advanced C++ STL 15
<algorithm> binary_search RndAccIt: 𝑂 lg 𝑛
𝑂(𝑛) otherwise
1
0
1 Incorrect usage
?
Advanced C++ STL 16
<algorithm> lower_bound RndAccIt: 𝑂 lg 𝑛
𝑂(𝑛) otherwise
2
6
10
If every element in the range is < val, r is returned
Advanced C++ STL 17
<algorithm> upper_bound RndAccIt: 𝑂 lg 𝑛
𝑂(𝑛) otherwise
5
6
Note: distance is 𝑂(1) for RndAccIt, 𝑂(𝑛) for FwdIt
▸ bool next_permutation(l, r)
▸ Rearranges the range to form the next lexicographically greater permutation
▸ If the range already contains the greatest permutation (elements in non-
increasing order), false is returned and the range is rearranged to form the
smallest permutation (elements in non-decreasing order)
Result:
12345
Result: 12354
4223:0 12435
....
54312
54321
Result:
2234:1 Advanced C++ STL 19
string
▸ When writing modern C++ programs,
Input:
avoid using arrays, C strings and raw pointers
abc def
creates an empty string
Output:
stops at space, new line or tab s is ""
s is "abc"
▸ string uses 0-based indexing
{} or () both ok
Output:
ghijkl
gxi
Advanced C++ STL 20
string
Append a character
Output: abcde
Concatenate Append
▸ Reverse a substring
Input Output
4
115 Case #1: 151
1051 Case #2: 1105
6233 Case #3: 6323
321 Case #4: 1023
Advanced C++ STL 24
Exercise - G091BB
Output:
Initial capacity: 0
1 2 4 4 8 8 8 8 16 16
▸ .size() returns an
unsigned integer
▸ Possible fixes:
don't
do this
it = l.erase(it) 𝑂(1)
Remove the element pointed by it
Returns an iterator pointing to the
next element after it
vector
0.42s
deque
1.50s
insert / erase
𝑂(lg 𝑛)
Input: 3 Output: 3
Input: 6 Output: 7
Input: 12 Output: None
Warning:
lower_bound(st.begin(), st.end(), x)
will compile but is O(n)
Advanced C++ STL 51
map<K, M>
▸ Sometimes it might be more useful to store extra information
other than the key
▸ map<K, M> is similar to set<K> but extra information of type M
is added to each key
If key is not found,
an insertion is performed
If key is found,
the value is updated
Advanced C++ STL 52
map<K, M>
▸ mp.find(k) returns an iterator to the element with key k if it
exists, returns mp.end() instead of key k does not exist
▸ The iterator points to the value type, which is a
pair<const K, M> with first being the key and
second being the mapped value
0 1 2 3 4 5 6 7 8 9 10
specify hasher class {4, 2}
{0, 6}
{10, 12}
0.48s
Key 2 3 5 7
0.01s Mapped 3 2 1 2
Value
freq.upper_bound(10)
== freq.end()
Input:
4
3 4 9 25
Output:
0 3 4 7 9 12 13 16 25 28 29 32 34 37 38 41
0.20s
20.8s
▸ set / map
▹ Insert: All validators remain valid
▹ Erase: Iterators to erased element(s) are invalidated
▸ unordered_set / unordered_map
▹ Insert: All iterators are invalidated if a rehash occurs
▹ Erase: Iterators to erased element(s) are invalidated
Advanced C++ STL 68
Iterator validity
▸ For all containers, iterators pointing to .end() will always
become invalid after a size change
Possible output:
0x16969d0
0x16969d0