C++ STL Algorithm

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)

2、关系(relational)

3、逻辑运算(logical)

 

 

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:

list来操作stl中的算法---其他的都相似!

详细解说 STL 排序(Sort)

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值