博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ STL Algorithm
阅读量:2359 次
发布时间:2019-05-10

本文共 15321 字,大约阅读时间需要 51 分钟。

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()

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):

The behavior of this template function is equivalent to:

 

 

 

5. 范围内元素替换 replace()

 

6. 从范围内删除指定值 remove()

从实现来看与erase()不同,通用算法中的remove()也与容器list的成员函数remove()不同,通用算法中的remove()并没有容器中删除东西而是把非指定的值位置提前替换掉指定值占用的位置。

 

 

应用函数而不具体指定值来判断要删除的元素 remove_if()

 

7. 统计范围内等于指定值的元素个数 count()

 

 

统计范围内满足条件的元素个数 count_if()

 

 

8. 查找范围内等于指定值的第一个元素的位置 find()

 

应用函数来查找 find_if()

 

9. 查找序列 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:

 

你可能感兴趣的文章
获取论文全文的13种方法
查看>>
把爱传递——写在第10次献血和成为一名义工之际
查看>>
应用随机过程概率模型导论(9th 英文版) Sheldon M.Ross 共享
查看>>
两个不错的 Matlab 时频分析工具箱
查看>>
【转】研究生必读→如何获得全文文献
查看>>
考博心得集锦——成功因素、相关准备工作
查看>>
考博联系导师的办法[转]
查看>>
考博考硕联系导师的注意事项
查看>>
2008北航博士考试总结【转】
查看>>
揭秘卡耐基•梅陇大学机器蛇
查看>>
【转】免费学术资源网址
查看>>
【转】免费进入学术数据库
查看>>
【转】matlab 使用的一点儿体会(for beginner)
查看>>
蚂蚁的哲学
查看>>
朴素的美丽、抑郁中的缕缕阳光——“红衣妹妹”于洋博文选粹
查看>>
最新的计算机方向的国际会议/期刊的排名
查看>>
《蚁群算法原理及其应用》(段海滨)附录Matlab源程序
查看>>
新的探索!
查看>>
小波图像分解与重构程序存在的问题与解决办法
查看>>
小波图像分解 Matlab 程序 - V2.0版
查看>>