STL基础--算法(不修改数据的算法)
不修改数据的算法
- count, min and max, compare, linear search, attribute
// 算法中Lambda函数很常用:
num = count_if(vec.begin(), vec.end(), [](int x){return x<10;});
bool lessThan10(int x) {
return x<10;
}
vector<int> vec = {9,60,90,8,45,87,90,69,69,55,7};
vector<int> vec2 = {9,60,70,8,45,87};
vector<int>::iterator itr, itr2;
pair<vector<int>::iterator, vector<int>::iterator> pair_of_itr;
// C++ 03: 一些算法可以在tr1或者boost中找到
vector<int> vec = {9,60,90,8,45,87,90,69,69,55,7};
计数 Counting
int n = count(vec.begin()+2, vec.end()-1, 69); // 2 元素值等于69的个数
int m = count_if(vec.begin(), vec.end(), [](int x){return x==69;}); // 3 元素满足谓词的个数
int m = count_if(vec.begin(), vec.end(), [](int x){return x<10;}); // 3
最大小值 Min and Max
itr = max_element(vec.begin()+2, vec.end()); // 90 返回第一个最大元素的迭代器 itr = max_element(vec.begin(), vec.end(),
[](int x, int y){ return (x%10)<(y%10);}); // 9 自定义比较函数 // 大多数算法有一个简单形式和一个更通用的形式 itr = min_element(vec.begin(), vec.end()); // 7
// 通用形式的min_element() pair_of_itr = minmax_element(vec.begin(), vec.end(), // {60, 69}
[](int x, int y){ return (x%10)<(y%10);});
// 返回一个pair, 包含第一个最小值和最后一个最大值
线性搜索(当数据未排序时使用)
// 返回第一个匹配的
itr = find(vec.begin(), vec.end(), 55); itr = find_if(vec.begin(), vec.end(), [](int x){ return x>80; }); itr = find_if_not(vec.begin(), vec.end(), [](int x){ return x>80; }); itr = search_n(vec.begin(), vec.end(), 2, 69); // 连续2个69
// 通用形式的search_n() // 搜索子串
vector<int> sub = {45, 87, 90};
itr = search( vec.begin(), vec.end(), sub.begin(), sub.end());
// 搜索第一个匹配的子串
itr = find_end( vec.begin(), vec.end(), sub.begin(), sub.end());
// 搜索最后一个匹配的子串
// 通用形式: search(), find_end() // 搜索任意一个
vector<int> items = {87, 69};
itr = find_first_of(vec.begin(), vec.end(), items.begin(), items.end());
// 搜索任意一个在items中的元素
itr = find_first_of(vec.begin(), vec.end(), items.begin(), items.end(),
[](int x, int y) { return x==y*4;});
// 搜索任意一个在items中的元素,且满足谓词 // 搜索相邻
itr = adjacent_find(vec.begin(), vec.end()); // 搜索相邻两个相同的元素
itr = adjacent_find(vec.begin(), vec.end(), [](int x, int y){ return x==y*4;});
// 通用版本,自定义谓词
范围比较
if (equal(vec.begin(), vec.end(), vec2.begin())) {
cout << "vec and vec2 are same.\n";
} if (is_permutation(vec.begin(), vec.end(), vec2.begin())) {
cout << "vec and vec2 have same items, but in differenct order.\n";
} pair_of_itr = mismatch(vec.begin(), vec.end(), vec2.begin());
// 找到第一个不同的元素
// pair_of_itr.first是vec的迭代器
// pair_of_itr.second是vec2的迭代器 //词典比较: 用"less than"逐元素比较
lexicographical_compare(vec.begin(), vec.end(), vec2.begin(), vec2.end());
// {1,2,3,5} < {1,2,4,5}
// {1,2} < {1,2,3} // 通用形式:
// equal(), is_permutation(), mismatch(), lexicographical_compare()
检查属性
is_sorted(vec.begin(), vec.end()); // 检查vec是否排序 itr = is_sorted_until(vec.begin(), vec.end());
// itr指向第一个不满足排序的元素
// 通用形式: is_sorted(), is_sorted_until() is_partitioned(vec.begin(), vec.end(), [](int x){return x>80;} );
// 检查vec是否由谓词的条件分成了两个部分(x>80) is_heap(vec.begin(), vec.end()); // 检查vec是否是一个堆,heap
itr = is_heap_until(vec.begin(), vec.end()); // 找到第一个不是堆的位置 // 通用形式: is_heap(), is_heap_until()
All, any, none
all_of(vec.begin(), vec.end(), [](int x) {return x>80} );
// 所有的元素都满足 any_of(vec.begin(), vec.end(), [](int x) {return x>80} );
// 任意一个元素满足 none_of(vec.begin(), vec.end(), [](int x) {return x>80} );
// 所有元素都不满足
STL基础--算法(不修改数据的算法)的更多相关文章
- STL基础--算法(修改数据的算法)
修改元素的算法 copy, move, transform, swap, fill, replace, remove vector<int> vec = {9,60,70,8,45,87, ...
- 大数据排序算法:外部排序,bitmap算法;大数据去重算法:hash算法,bitmap算法
外部排序算法相关:主要用到归并排序,堆排序,桶排序,重点是先分成不同的块,然后从每个块中找到最小值写入磁盘,分析过程可以看看http://blog.csdn.net/jeason29/article/ ...
- STL基础--算法(已排序数据的算法,数值算法)
已排序数据的算法 Binary search, merge, set operations 每个已排序数据算法都有一个同名的更一般的形式 vector vec = {8,9,9,9,45,87,90} ...
- STL理论基础、容器、迭代器、算法
一.STL基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段 ...
- stl中的容器、迭代器和算法----vector中的find实现
来源 http://blog.csdn.net/huangyimin/article/details/6133650 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的 ...
- 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法
[题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...
- 【C/C++学院】0723-32位与64位/调戏窗体程序/数据分离算法/内存检索/二分查找法/myVC
[送给在路上的程序猿] 对于一个开发人员而言,能够胜任系统中随意一个模块的开发是其核心价值的体现. 对于一个架构师而言,掌握各种语言的优势并能够运用到系统中,由此简化系统的开发,是其架构生涯的第一步. ...
- 数据预测算法-ARIMA预测
简介 ARIMA: AutoRegressive Integrated Moving Average ARIMA是两个算法的结合:AR和MA.其公式如下: 是白噪声,均值为0, C是常数. ARIMA ...
- python基础__十大经典排序算法
用Python实现十大经典排序算法! 排序算法是<数据结构与算法>中最基本的算法之一.排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大, ...
随机推荐
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
Struts Problem Report Struts has detected an unhandled exception: Messages: Write operations are not ...
- java一些必会算法
经典算法的Java实现 (1)河内塔问题: 42 (2)费式数列 43 (3)巴斯卡(Pascal)三角形 44 (4)蒙地卡罗法求 PI 45 (5)最大公因数.最小公倍数 46 (6)阿姆斯壮数 ...
- Hierarchical RNN
https://blog.csdn.net/liuchonge/article/details/73610734 https://blog.csdn.net/triplemeng/article/de ...
- 关于js 异步回调的一些方法
http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html
- 使用w uptime vmstat top sar nload 等命令查看系统负载
1. w 和uptime,查看cpu的使用率: 2.vmstat 命令,查看更细的物理设备使用状况: 3.top 命令: top -c 可具体查看命令及路径: top -bn1 静太显示一条命令, ...
- hibernate(一)
hibernate介绍 jdbc缺点 1代码结构防繁琐,面向纯sql语句的编程,对于查询而言只要查询数据库的一张表,不需有如下编码 2有connection缓存,没有数据缓存 3.事务自动开启,有 ...
- MVC框架的理解(配置文件一次编写,所有的java代码都可以运行)
- 每天进步一点点-一切皆对象/一次编写,到处运行/bean工厂
只要这个配置文件一写,其他所有的java类都可以用 用法1.直接在类中getBeans,然后调用beans的方法 用法2.将这些bean进行注入,基于xml的注入<property name=& ...
- hiveserver 占用内存过大的问题
今天为了求解hiveserver占用内存过大的问题,特地加了hive在apache的邮件列表,讨论半天.特别说的是 里面的人确实很热情啊 ,外国人做事确实很认真,讨论帖发的时候都狠详细. 粘出一些记录 ...
- Gravitee.io Access Management docker-compose运行
Gravitee.io 官方提供的docker-compose 快速运行的方式 默认ui 账户 admin adminadmin 环境准备 docker-compose 文件 # # Copyrigh ...