All the code in this week is compiled with
-std=c++11
.
std::vector
- Sort Demo:
size
,operator[]
- constructor
push_back
- clear
insert
anderase
- Sort Demo:
std::deque
std::list
- A doubly linked list
- push..., pop...
- size, empty...
- insert, erase
std::forward_list
- A singly linked list
std::forward_list
vs.std::list
- insert (at the beginning), erase
std::set
- An unordered list of elements
- insert, erase
- size, empty...
- Custom Type:
operator<
std::map
- Key-Value pair
- Access
operator[]
,find
std::pair
andstd::tuple
- insert, erase
- size, empty
std::unordered_set
- A hashmap-based container
- Hashing
- insert, erase
- size, empty...
- Custom Type:
hash
and equality
std::unordered_map
- Key-Value pair
- insert, erase
- size, empty...
- Custom Type:
hash
and equality
multiset
,multimap
Question: why they have similar APIs?
- Why Adaptor?
std::stack
- Based on vector by default
top
push
,pop
std::queue
- Based on deque by default
front
,back
push
,pop
std::priority_queue
- Based on vector by default
- Application: heap sort
- Range-based for loop
- Demo C++Insights: What's the mechanism?
- Behaviors
- Dereferencing the iterator to read a value:
operator*
- Advancing the iterator from one position to the next:
operator++
- Comparing two iterators for equality:
operator==
- Dereferencing the iterator to read a value:
- Types of Iterator (with their minimum requirements)
- InputIterator: single-pass, read-only
- OutputIterator: single-pass, write-only
- ForwardIterator: multi-pass, read, uni-direction
- BidirectionalIterator: multi-pass, read, multi-direction
- RandomIterator: multi-pass, read, random pos
- Const Iterator
- Iterator Adaptor
std::back_inserter
std::ostream_iterator
- Turns out to be useful when we know Algorithm
- Additional Resources
count_if example: count the number of items that make
Pred == true
- Function Pointer
- Function Object
operator()
- Key Characteristics: has states
- Higher Order Programming: functions as parameters, currying (
std::bind
, lambda)
- Lambda
- Simplify Function Object
- Syntax:
[](){}
- Capture
- Params List
- Function Body
mutable
std::accumulate
std::count
_if
:std::count_if
,std::copy_if
std::sort
std::shuffle
- How to design an efficient random shuffling algorithm?
- Key Property: Random
std::binary_search
,std::lower_bound
,std::upper_bound
std::remove
andcontainer.erase
paradigm, andstd::erase
(since C++20)std::next
,std::distance
If you want to learn more about algorithm, refer to CppCon 2018: Jonathan Boccara “105 STL Algorithms in Less Than an Hour”
This work was largely inspired by the CS106L course readings. If you want to learn more about C++, you can start here.