1.find(first, last, value)

  • 头文件:algorithm
  • 参数:前两个参数是“表示元素范围的迭代器”,第三个是一个值
  • 说明:find 将范围中进行寻找。搜索失败:如果范围中无匹配元素,则find返回第二个参数来表示。搜索成功:返回指向第一个等于给定值的迭代器。
  • 示例:
  •  #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std; int main() { int v1 = ;
    int a1 = ; vector<int> v{ , , , , };
    int a[] = { , , , , }; // 可用 auto 替代
    vector<int>::iterator result1 = find(v.begin(), v.end(), v1);
    int *result2 = find(begin(a), end(a), a1); if (result1 == v.end()) {
    cout << "v not found "<< v1 << endl;
    } else {
    cout << "v found " << v1 << endl;
    } if (result2 == end(a)) {
    cout << "a not found " << a1 << endl;
    } else {
    cout << "a found " << a1 << endl; // 可见 *(end(a))!=4. 而 *(begin(a))==0
    } getchar();
    return ;
    }

  

2.find_if(first, last, p)

  • 头文件:algorithm
  • 参数:前两个参数表示范围,第三个参数是一个谓词 (只接受单一参数的函数)
  • 说明:对输入序列中的每个元素条用给定的这个谓词。搜索失败:返回尾迭代器。搜索成功:返回第一个使谓词返回非0值的元素。
  • 示例:
  •  #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std; int judge(int i) { // 一元谓词,只能传入一个参数
    return i % ;
    } int main() { vector<int> v{ , , , }; // 可用 auto 替代
    vector<int>::iterator result1 = find_if(v.begin(), v.end(), judge);// 返回第一个奇数 if (result1 != v.end()) {
    cout << "第一个奇数:"<< *result1 << endl;
    } else {
    cout << "无奇数 " << endl;
    } getchar();
    return ;
    }

3.back_inserter(Container& c )

  • 头文件:iterator
  • 参数:接受一个指向容器的引用,返回一个与该容器绑定的插入迭代器。
  • 说明:当我们通过此迭代器赋值时,赋值运算符会调用 push_back 将一个具有给定值的元素添加到容器
  • 示例:
  •  #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<iterator>
    using namespace std; int main() { vector<int> v4;
    auto iter = back_inserter(v4);
    //back_insert_iterator<vector<int>> iter = back_inserter(v4);
    *iter = ;
    for (auto elem : v4) {
    std::cout << elem << " ";
    } getchar();
    return ;
    }

4.fill(first, last, value),fill_n(first, count, value)

  • 头文件:algorithm
  • 参数:fill——接受一对迭代器表示一个范围,还接受一个值作为第三个参数,将给定的这个值赋予输入序列中的每个元素。fill_n——接受一个单迭代器、一个计数值和一个值。它将给定值赋予迭代器指向的元素开始的指定个元素。
  • 说明:fill 可以对空容器操作,但是 fill_n 不能对空容器操作(但是可用 back_inserter 解决)。
  • 示例:
  •  #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<iterator>
    using namespace std; int main() { /// fill
    vector<int> v{ , , , , , , , , , };
    //vector<int> v; // 容器为空。可以使用
    fill(v.begin(), v.end(), );
    for (auto elem : v) {
    std::cout << elem << " ";
    }
    cout << endl; ///fill_n
    vector<int> v2{ , , , , , , , , , };
    fill_n(v2.begin(), , );
    for (auto elem : v2) {
    std::cout << elem << " ";
    }
    cout << endl; vector<int> v3; // 容器为空(使用错误)
    //fill_n(v3.begin(), 5, 99); // 报错
    //使用back_inerter解决
    fill_n(back_inserter(v3), , -);
    for (auto elem : v3) {
    std::cout << elem << " ";
    } getchar();
    return ;
    }

5.copy(first, last, d_first)

  • 头文件:algorithm
  • 参数:前两个表示输入范围,第三个表示目的序列的起始位置。传递给copy的目的序列至少要包含与输入序列一样多的元素!
  • 说明:copy返回值:copy返回的是其目的位置迭代器(递增后)的值。
  • 示例:
  •  #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<iterator>
    using namespace std; int main() { vector<int> v{ , , , , , , , , , };
    vector<int> v2;
    //auto result = copy(v.begin(), v.end(), v2.begin());//报错。v2为空
    auto result = copy(v.begin(), v.end(), back_inserter(v2));
    for (auto elem : v2) {
    cout << elem << " ";
    }
    cout << endl; //我们可以用copy实现内置数组的拷贝
    int a1[] = { ,,,,,,,,, };
    int a2[sizeof(a1) / sizeof(*a1)]; //a2与a1大小一样
    //ret指向拷贝到a2的尾元素之后的位置
    auto ret = copy(begin(a1), end(a1), a2); // 把a1的内容拷贝给a2
    for (auto elem : a2) {
    cout << elem << " ";
    }
    cout << *(--ret) << " "; // 输出9。ret恰好指向拷贝到a2的尾元素之后的位置。 getchar();
    return ;
    }

6.replace(first, last, old_value, new_value)

  • 头文件:algorithm
  • 参数:前两个是迭代器,表示输入序列,后两个一个是要搜索的值,另一个是新值。

7.sort(first, last),sort(first, last, comp)

  • 头文件:algorithm
  • 参数:排序范围。comp——比较函数对象(即满足比较(Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 ​true 。比较函数的签名应等价于如下:bool cmp(const Type1 &a, const Type2 &b);
  • 说明:默认情况 sort 是按照 < 运算符进行运算
  • 示例:
  •  #include <algorithm>
    #include <functional>
    #include <array>
    #include <iostream> int main()
    {
    std::array<int, > s = { , , , , , , , , , }; // 用默认的 operator< 排序
    std::sort(s.begin(), s.end());
    for (auto a : s) {
    std::cout << a << " "; //0 1 2 3 4 5 6 7 8 9
    }
    std::cout << '\n'; // 用标准库比较函数对象排序
    std::sort(s.begin(), s.end(), std::greater<int>());
    for (auto a : s) {
    std::cout << a << " "; //9 8 7 6 5 4 3 2 1 0
    }
    std::cout << '\n'; // 用自定义函数对象排序
    struct {
    bool operator()(int a, int b) const
    {
    return a < b;
    }
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    for (auto a : s) {
    std::cout << a << " "; //0 1 2 3 4 5 6 7 8 9
    }
    std::cout << '\n'; // 用 lambda 表达式排序
    std::sort(s.begin(), s.end(), [](int a, int b) {
    return b < a;
    });
    for (auto a : s) {
    std::cout << a << " "; //9 8 7 6 5 4 3 2 1 0
    }
    std::cout << '\n';
    }

c++基础(四)—— 泛型算法的更多相关文章

  1. C++基础之泛型算法

    标准库并未给每个容器添加大量功能,因此,通过大量泛型算法,来弥补.这些算法大多数独立于任何特定的容器,且是通用的,可用于不同类型的容器和不同的元素. 迭代器使得算法不依赖容器,但是算法依赖于元素的类型 ...

  2. C++ 泛型算法

    <C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...

  3. C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法

    大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ●  find算法,算法接受一对迭代 ...

  4. Chapter10:泛型算法

    泛型算法的基础是迭代器. 迭代器令算法不依赖于容器,但是算法依赖于元素类型的操作.也即:算法永远不会执行容器的操作. 那么,如果想向容器中添加元素或者执行其他的一些操作呢?标准库提供了插入迭代器来完成 ...

  5. c++11の泛型算法

    一.泛型算法泛型算法这个概念是针对容器操作的,我们知道,c++11的顺序容器有vector,list,deque等,对于这些容器,c++11并没给出相应的增删改查方法,而是定义了一组泛型算法 一般的泛 ...

  6. 算法入门:最大子序列和的四种算法(Java)

    最近再学习算法和数据结构,推荐一本书:Data structures and Algorithm analysis in Java 3rd 以下的四种算法出自本书 四种最大子序列和的算法: 问题描述 ...

  7. [C++ Primer] : 第10章: 泛型算法

    概述 泛型算法: 称它们为"算法", 是因为它们实现了一些经典算法的公共接口, 如搜索和排序; 称它们是"泛型的", 是因为它们可以用于不同类型的元素和多种容器 ...

  8. C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构

    STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...

  9. c++ primer 11 泛型算法

    使用泛型算法必须包含头文件#inlucde <algorithm> 标准库还定义一组泛化的算术算法,其命名习惯与泛型算法相同,包含头文件#include <numeric> f ...

  10. 机器学习(四) 分类算法--K近邻算法 KNN (上)

    一.K近邻算法基础 KNN------- K近邻算法--------K-Nearest Neighbors 思想极度简单 应用数学知识少 (近乎为零) 效果好(缺点?) 可以解释机器学习算法使用过程中 ...

随机推荐

  1. C# 监控网速

    主要有两个类,其一是NetworkAdapter,该类的作用是获取本机网络适配器列表,并且可以通过该类的属性获取当前网速数据:其二是NetworkMonitor,该类是通过.NET的Performan ...

  2. 18-ESP8266 SDK开发基础入门篇--TCP 服务器 RTOS版,串口透传,TCP客户端控制LED

    https://www.cnblogs.com/yangfengwu/p/11112015.html 先规定一下协议 aa 55 02 01 F1 4C 控制LED点亮  F1 4C为CRC高位和低位 ...

  3. 使用localstorage.setItem()存储对象

    使用localstorage.setItem(name,value)存储JSON对象时会发现浏览器存储的内容为[object,object],并不是我们想要的内容,这是因为我们在存储的时候没有进行类型 ...

  4. (10)Go结构体struct

    结构体 Go语言中的基础数据类型可以表示一些事物的基本属性,但是当我们想表达一个事物的全部或部分属性时,这时候再用单一的基本数据类型明显就无法满足需求了,Go语言提供了一种自定义数据类型,可以封装多个 ...

  5. nginx中的upstream使用

    upstream的基本使用 upstream admin{server 127.0.0.1:9090 down;server 127.0.0.1:8080 weight=2;server 127.0. ...

  6. [RoarCTF]Easy Calc

    目录 [RoarCTF]Easy Calc 知识点 1.http走私绕过WAF 2.php字符串解析特性绕过WAF 3.绕过过滤写shell [RoarCTF]Easy Calc 题目复现链接:htt ...

  7. ubuntu之路——day15.1 只用python的numpy在底层检验参数初始化对模型的影响

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...

  8. photoshop 的安装破解

    最近学习需要用到photoshop,但是photoshop试用期只有30天,于是尝试破解photoshop.参考了网上的很多博客,失败了好几次,终于找到一篇靠谱的博客,很顺利的成功了.在这里记录一下, ...

  9. spring 技术内幕读书笔记1

    1 在 java 应用开发中,往往会涉及复杂的对象耦合关系,在 代码中处理这些耦合关系,对代码的维护性和应用扩展性会带来许多不便.通过使用spring 的 IOC 容器,可以对这些耦合关系实现一个文本 ...

  10. 【原创】MongoDB安装配置详解(标注两个坑)

    1.下载安装 3.4正式版([坑]不要最新版,有可能进度卡在这个位置不动,等了半个小时也没什么反映,) http://downloads.mongodb.org/win32/mongodb-win32 ...