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实现十大经典排序算法! 排序算法是<数据结构与算法>中最基本的算法之一.排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大, ...
随机推荐
- linux 禁ping和开启ping方法
Linux 禁ping和开启ping操作# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all如果要恢复,只要:# echo 0 > /pro ...
- render finished
- 直接复制浏览器Request headers中的进行copyheaders进行转换
先导入函数库 from copyheaders import headers_raw_to_dict 然后复制请求头 headers = b'''accept: application/json, t ...
- epoll c++封装
#ifndef _BFC_EPOLL_FLOW_HPP_ #define _BFC_EPOLL_FLOW_HPP_ #include <string.h> #include <err ...
- Codewars
You can vote on other Codewarrior's solutions to help uncover the best ones. There are 2 choices for ...
- linux----CenterOS7中在线安装jdk
summary: 一直以来,都在windows上玩java,今天是一个具有里程碑的一天,感觉正式踏入进入了linux大门. 原来一直以为在linux上安装jdk,需要去官网下载适合linux的jdk, ...
- XML之命名空间的作用(xmlns)
http://www.w3school.com.cn/xml/xml_namespaces.asp http://blog.csdn.net/zhch152/article/details/81913 ...
- MySQL--Semi-join(半连接)优化策略
Semi-join(半连接)半连接主要场景:检查一个结果集(外表)的记录是否在另外一个结果集(字表)中存在匹配记录,半连接仅关注”子表是否存在匹配记录”,而并不考虑”子表存在多少条匹配记录”,半连接的 ...
- drone 1.0 新的定时任务界面&&构建任务支持重启
drone 1.0 的定时任务是一个不错的功能,早期的版本是必须使用cron 表达式的 最近发布的版本支持通过配置就可以了,很方便,只是目前比较简单的,支持小时. 天.周.月.年的模式 环境准备 do ...
- python结合redis模拟队列
实在无聊就写了个很小的python程序用来实现模拟redis队列的代码如下: redis_lpush.py #!/usr/bin/python3 import time import redis ...