- inheritance
- unless the destructor
~Base
is markedvirtual
, the destructor~Child
won't be called ondelete base
wherebase
is of typeChild*
.
- unless the destructor
-
algorithm
-
lower_bound(first, last, value)
andupper_bound(first, last, value)
note that we use theset<int> s { 1, 3, 6, 10, 13, 32, 45 }; // first element x such that x >= 13 s.lower_bound(13); // 13 // first element x such that x > 13 s.upper_bound(13); // 32
set
methodlower_bound
notstd::lower_bound
because for non random access iterators such those provided byset
and other associative containers makes the algorithm (binary search) used bystd::lower_bound
do number of iterator increments that is linear in N so the memberlower_bound
functions should be preferred.
-
-
containers:
- iterator adaptors:
-
back_inserter(Container)
needs a container withpush_back
method -
front_inserter(Container)
needs a container withpush_front
method -
inserter(Container, Container::iterator)
needs a container withinsert
method
-
- method
size()
in some containers (e.g. list) is NOT$O(1)$ but the methodempty()
in all containers is$O(1)$ - sequence container:
vector
,deque
,list
- all sequence container except
array
has a count-value constructorvector<int> v(100, 5) // declare v as 100 5's vector
-
vector
- random access
$O(1)$ - random access iterator
- insertion/deletion at end
$O(1)$ - insertion/deletion elsewhere
$O(n)$
- random access
-
deque
- random access
$O(1)$ BUT slower than vector - random access iterator
- insertion/deletion at begin/end
$O(1)$ - insertion/deletion elsewhere
$O(n)$ - elements are NOT stored contiguously
- random access
-
list
- random access
$O(n)$ - bidirectional iterator
- insertion/deletion at begin/end/known pos
$O(1)$ ] - method
merge
merges two sorted lists- does nothing if
other
refers to the same objects as*this
- if
*this
orother
is not sorted, the behavior is undefined
- does nothing if
- method
splice
moves element(s) from another listlist<int> x { 1, 5, 2, 6 }; list<int> y { 3, 7, 4 }; x.splice(x.end(), y); // move all y elements to th end of x x.splice(x.begin(), y, y.begin()); // move the y element at y.begin() to the front of x x.splice(x.begin(), y, y.begin(), y.end()); // equivalent to x.splice(x.begin(), y)
- list method
unique
removes consecutive duplicate elementslist<int> x { 1, 2, 1, 1, 3, 3 }; x.unique(); // x is now [ 1 2 1 3 ]
- random access
- all sequence container except
- associative container:
set
/map
(unimodal) andmultiset
/multimap
(multimodal)- search, removal, and insertion is
$O(log(n))$ - ordered and by default is
std::less
so elements are sorted ascendingly - bidirectional iterator
-
set
: ordered set of unique objects -
map
: ordered set of key-value pairs with unique keys -
multiset
: ordered collection of objects, multiples are allowed -
multimap
: ordered collection of key-value pairs, multiples are allowed
- search, removal, and insertion is
- unordered associative container:
unordered_set
/unordered_map
(unimodal) andunordered_multiset
/unordered_multimap
(multimodal)- search, removal, and insertion is on average
$O(1)$ and$O(n)$ at worst. - bidirectional iterator
-
unordered_set
: unordered set of unique objects, hashed by keys -
unordered_map
: unordered set of key-value pairs with unique keys, hashed by keys -
unordered_multiset
: unordered collection of objects, multiples are allowed, hashed by keys -
unordered_multimap
: unordered collection of key-value pairs, multiples are allowed, hashed by keys
- search, removal, and insertion is on average
- container adapters
stack
/queue
/priority_queue
- do not support iterators
- method
push
/pop
is common in them - stack container:
- lifo ordering (last in first out)
- queue containr
- access at
front()
andback()
- fifo ordering (first in first out)
- access at
- priority queue
- insertion order is irrelevant
-
top()
is the element such that for every element x in the containerCompare(x, top())
istrue
. - By default
Compare
isstd::less
sotop()
is the largest element in the container.
- iterator adaptors:
- UML
- C++ access modifier correspondence
- private:
-
- protected:
#
- public:
+
- private:
- data type notation
- id: int # width: double # length: double
- parameter and return type notation
+ setWidth(w: double): void + getWidth(): double
- no return type for constructors or destructors
- C++ access modifier correspondence
Last active
January 20, 2025 00:54
-
-
Save faresbakhit/0644cf616a3f17046f8eea944d6549d4 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment