不修改数据的算法

  • 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};
  1. 计数 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
  2. 最大小值 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, 包含第一个最小值和最后一个最大值
  3. 线性搜索(当数据未排序时使用)

    //    返回第一个匹配的
    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;});
    // 通用版本,自定义谓词
  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()
  5. 检查属性

    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()
  6. 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基础--算法(不修改数据的算法)的更多相关文章

  1. STL基础--算法(修改数据的算法)

    修改元素的算法 copy, move, transform, swap, fill, replace, remove vector<int> vec = {9,60,70,8,45,87, ...

  2. 大数据排序算法:外部排序,bitmap算法;大数据去重算法:hash算法,bitmap算法

    外部排序算法相关:主要用到归并排序,堆排序,桶排序,重点是先分成不同的块,然后从每个块中找到最小值写入磁盘,分析过程可以看看http://blog.csdn.net/jeason29/article/ ...

  3. STL基础--算法(已排序数据的算法,数值算法)

    已排序数据的算法 Binary search, merge, set operations 每个已排序数据算法都有一个同名的更一般的形式 vector vec = {8,9,9,9,45,87,90} ...

  4. STL理论基础、容器、迭代器、算法

    一.STL基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段 ...

  5. stl中的容器、迭代器和算法----vector中的find实现

    来源 http://blog.csdn.net/huangyimin/article/details/6133650 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的 ...

  6. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

  7. 【C/C++学院】0723-32位与64位/调戏窗体程序/数据分离算法/内存检索/二分查找法/myVC

    [送给在路上的程序猿] 对于一个开发人员而言,能够胜任系统中随意一个模块的开发是其核心价值的体现. 对于一个架构师而言,掌握各种语言的优势并能够运用到系统中,由此简化系统的开发,是其架构生涯的第一步. ...

  8. 数据预测算法-ARIMA预测

    简介 ARIMA: AutoRegressive Integrated Moving Average ARIMA是两个算法的结合:AR和MA.其公式如下: 是白噪声,均值为0, C是常数. ARIMA ...

  9. python基础__十大经典排序算法

    用Python实现十大经典排序算法! 排序算法是<数据结构与算法>中最基本的算法之一.排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大, ...

随机推荐

  1. doc转html

    Doc2Html  li标签没闭合 在线Word转HTML  style样式

  2. CSS使用方法

    CSS行内样式: 在开始标签内添加style样式属性 如:<p style="color:red;">内容</p> CSS内部样式: 内部样式(嵌入样式), ...

  3. 1001.A+B Format (20)题目解答

    前言 最开始看到这个题目,我的第一个想法是有没有那种输出格式可以直接拿来用的,然后我百度了一下,想偷懒,然而并没有这种东西.只好动动自己的脑子了. 关于GitHub 这个问题,当初我弄了五天才建立好联 ...

  4. debian 配置linuxptp 软件时间戳

    编程之路刚刚开始,错误难免,希望大家能够指出. ntp,ptp,ntp,ptp 本文只说软件时间戳 先上几个推荐的网址,可以更好的了解ptp: https://docs.fedoraproject.o ...

  5. LeetCode – Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  6. Redis和MySQL的结合方案

    转载:http://m.blog.csdn.net/article/details?id=50586990 方案一: 程序同时写Redis和MySQL读Redis 方案二: 程序写MySQL, 使用G ...

  7. msyql开启慢查询以及分析慢查询

    慢查询的用途是用来发现执行时间长的查询语句,以便对这些语句进行优化 [mysqld] #在这里面增加,其它地方无效 #server-id=1 #log-bin=master-bin slow_quer ...

  8. Primitives vs Objects

    这里首先我们要了解什么是primitives 和 objects 其实理解起来很简单. 如果我们懂.NET开发就会知道C#中的值类型和引用类型. primitives variables contai ...

  9. 收藏一篇 Python 文本框操作命令

    原文地址:https://www.cnblogs.com/onlyfu/archive/2013/03/07/2947473.html 属性(Options) background(bg) borde ...

  10. laravel中resource资源路由方法

    新增的 resource 方法将遵从 RESTful 架构为用户资源生成路由.该方法接收两个参数,第一个参数为资源名称,第二个参数为控制器名称. Route::resource('users', 'U ...