1. 复制一个范围的元素 copy()
Copies the elements in the range [first,last) into a range beginning at result.
应用:接收标准输入保存到容器;输出容器内的元素。
copy(istream_iterator<T>(cin), istream_iterator<T>(), back_insert_iterator< vector<int> >(num))
copy(A.begin(), A.end(), ostream_iterator(cout, " "));
2. 从最后的元素开始复制 copy_backward()
http://www.cplusplus.com/reference/algorithm/copy_backward/
Copy range of elements backwards
Copies the elements in the range [first,last) into a range whose last element is result. The function begins by copying *(last-1) into *(result-1), and then follows backwards by the elements preceeding these, until first is reached (and including it).
The behavior of this function template is equivalent to:
3. 容器元素累加 accumulate()
accumulate(numbers,numbers+3,init);
Note: 第3个参数的类型会影响返回的结果,如果作浮点型的运算,第3个参数应该为0.0而不是0。
4. 将函数应用到一个范围内的所有元素 for_each()
Applies function f to each of the elements in the range [first,last):
http://www.cplusplus.com/reference/algorithm/for_each/
The behavior of this template function is equivalent to:
5. 范围内元素替换 replace()
http://www.cplusplus.com/reference/algorithm/replace/
6. 从范围内删除指定值 remove()
http://www.cplusplus.com/reference/algorithm/remove/
从实现来看与erase()不同,通用算法中的remove()也与容器list的成员函数remove()不同,通用算法中的remove()并没有容器中删除东西而是把非指定的值位置提前替换掉指定值占用的位置。
应用函数而不具体指定值来判断要删除的元素 remove_if()
http://www.cplusplus.com/reference/algorithm/remove_if/
7. 统计范围内等于指定值的元素个数 count()
http://www.cplusplus.com/reference/algorithm/count/
统计范围内满足条件的元素个数 count_if()
http://www.cplusplus.com/reference/algorithm/count_if/
8. 查找范围内等于指定值的第一个元素的位置 find()
http://www.cplusplus.com/reference/algorithm/find/
应用函数来查找 find_if()
http://www.cplusplus.com/reference/algorithm/find_if/
9. 查找序列 search()
http://www.cplusplus.com/reference/algorithm/search/
Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element.
10 排序 sort()
11 Function Objects
http://www.cplusplus.com/reference/std/functional/
不以独立的函数加以定义而采用function objects是出于效率的考虑。可以令call运算符成为inline,消除了“通过函数指针来调用函数”的额外开销。
functional objects分为:
1、算术运算(arithmetic)
plus | Addition function object class (class template) |
minus | Subtraction function object class (class template) |
multiplies | Multiplication function object class (class template) |
divides | Division function object class (class template) |
modulus | Modulus function object class (class template) |
negate | Negative function object class (class template) |
2、关系(relational)
equal_to | Function object class for equality comparison (class template) |
not_equal_to | Function object class for non-equality comparison (class template) |
greater | Function object class for greater-than inequality comparison (class template) |
less | Function object class for less-than inequality comparison (class template) |
greater_equal | Function object class for greater-than-or-equal-to comparison (class template) |
less_equal | Function object class for less-than-or-equal-to comparison (class template) |
3、逻辑运算(logical)
logical_and | Logical AND function object class (class template) |
logical_or | Logical OR function object class (class template) |
logical_not | Logical NOT function object class (class template) |
binder adapter (绑定配接器)
将function object的参数绑定至某个特定值身上,是binary(二元)function object转化为unary(一元)function object。
bind1st将指定值绑定至第一操作数,bind2nd将指定值绑定至第二操作数。
back_inserter() 以容器的push_back()函数取代assignment运算符;
inserter() 以容器的inseter() 函数取代assignment运算符;
front_inserter() 以容器的push_front()函数取代assingment运算符。
mem_fun
将成员函数转为函数对象
例:和transform结合利用string::length获得string数组每个成员长度
http://www.cplusplus.com/reference/std/functional/mem_fun/
ptr_fun
函数指针转函数对象
例:和accumulate结合利用atoi,累加保存为char字符串的数组
http://www.cplusplus.com/reference/std/functional/ptr_fun/
Hash Set
http://www.moosechips.com/2008/10/using-gcc-c-hash-classes-with-strings/
Reference: