1. Two functions can have the same name if they have different parameters
2. The compiler will use the one whose parameters match the ones you pass in
重载一个operator + ,这个operator+函数被定义为ruler类的成员函数
1. temp是一个新的ruler常量用来储存相加后的结果
2. feet和inches分别表示是英尺和英寸,类似磅和便士,(但是12英寸=1英尺)所以是this的feet和ru的feet相加,this的inches和ru的inches相加
这种重载作用大概就是指本来"==","<",">"有定义了,但是引入了英寸和英尺两个单位,所以重新定义这三个比较符号
There are 3 types of operators:
Prefix-unary, postfix-unary and binary
E.g. -a would be prefix-unary, a++ would be postfix-unary and a+b would be binary
前缀一元运算符,后缀一元运算符,二元运算符
In overloading, we provide multiple implementations of the same function for different parameters
With templates, we provide one implementation that behaves similarly for different parameters (still implementing one function)
为了解决返回多个objects的问题?
Contains three main parts:
3. Algorithms: uses iterator(s) (usually 2 and often some method as parameters) to give their results
Vector is essentially automatic resizing array 自动调整数组size
vector
automatic resizing arrays (i.e. O(1) for accessing/updating elements and adding to the end – adding elements elsewhere cost O(n))
arrays (same complexity) Faster than vector due to less overhead
More advanced automatic resizing array that allows inserting/deleting at the start as well in O(1) time Slower and have more overhead than vector
balanced trees (O(log n) accessing/updating/adding/removing elements)
doubled-linked list (O(1) traversing/updating/adding/removing elements – can’t really access)
single-linked list
Things are stored in ascending order (based on <) by default
You can do it with other orderings, but it looks a bit ugly…
Iterators are objects for accessing/manipulating the data in their container Multiple levels of them:
Random access, bidirectional, forward, input = output
(Input and output are at the same level)
Syntax looks a little strange so we need to explain it
1. Multiset/vector class has a member object called iterator
2. In this example, the multiset/vector template is using integers
3. So the iterator will point to an integer within the multiset/vector
这里it4是没有明确指代的 不能用auto it4 = store2.end();
而应该重新定义it4 :it4 = store2.end();
再去比较
可以用function