• C++ std::find()实例讲解


    find() 作为 STL 函数

    find() 是一个 STL 函数,它位于 头文件下,它返回一个迭代器,指向范围内搜索元素的第一次出现。

    用法:

    InputIterator find(
        InputIterator first, 
        InputIterator last, 
        const T& val);
    

    其中,

    • InputIterator first- 搜索范围开始的迭代器
    • InputIterator last- 到搜索范围结束的迭代器
    • const T& val- 要搜索的数据类型 T 的值

    什么是输入迭代器?
    迭代器到我们找到搜索元素的范围的第一个位置。如果未找到搜索元素,则将迭代器返回到末尾

    返回类型: bool

    无论是否找到搜索元素,都使用上述语法搜索相应范围内的元素。

    时间复杂度:线性时间,O(n)

    之间的区别binary_search()和 find() 函数

    • std::binary_search() 函数返回布尔值,告诉它是否找到。它不会返回位置。但是,std::find() 也搜索位置。它返回一个迭代器到第一个位置。
    • std::binary_search() 在 O(logn) 时间内搜索是否 std::find() 在线性时间内搜索。

    范例1:当搜索到的元素被找到并且在搜索范围内只有一个实例时

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. vector<int> arr{ 1, 2, 3, 8, 4, 3 };
    6. int searching_element = 8;
    7. vector<int>::iterator it;
    8. //starting iterator of range= arr.begin()
    9. //end iterator of range =arr.end()
    10. it = find(arr.begin(), arr.end(), searching_element);
    11. if (it != arr.end())
    12. cout << searching_element << " is at position:" << it - arr.begin() << endl;
    13. else
    14. cout << searching_element << "not found";
    15. return 0;
    16. }

    输出:

    8 is at position:3
    

    在上面的程序中,我们检查了搜索元素,发现它在3rd索引(0-索引)

    范例2:当搜索到的元素被找到并且在搜索范围内有多个实例时

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. vector<int> arr{ 1, 2, 3, 8, 4, 3 };
    6. int searching_element = 3;
    7. vector<int>::iterator it;
    8. //starting iterator of range= arr.begin()
    9. //end iterator of range =arr.end()
    10. it = find(arr.begin(), arr.end(), searching_element);
    11. if (it != arr.end())
    12. cout << searching_element << " is at position:" << it - arr.begin() << endl;
    13. else
    14. cout << searching_element << "not found";
    15. return 0;
    16. }

    输出:

    3 is at position:2 
    

    在上面的例子中,我们在数组中搜索 3,该数组有两个实例,一个在位置索引 2,另一个在位置 5。由于 std::find()在找到搜索元素时停止搜索,因此它返回索引 2(检查我们如何找到它返回的迭代器的位置)。

    范例3:当在搜索范围内未找到搜索元素时

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. vector<int> arr{ 1, 2, 3, 8, 4, 3 };
    6. int searching_element = 7;
    7. vector<int>::iterator it;
    8. //starting iterator of range= arr.begin()
    9. //end iterator of range =arr.end()
    10. it = find(arr.begin(), arr.end(), searching_element);
    11. if (it != arr.end())
    12. cout << searching_element << " is at position:" << it - arr.begin() << endl;
    13. else
    14. cout << searching_element << " not found";
    15. return 0;
    16. }

    输出:

    7 not found
    

    在上述情况下,搜索元素不存在,这就是它返回 arr.end() 的原因,这意味着迭代器到范围的末尾。

    好了, 以上是本文所有内容,希望对大家有所帮助,也希望大家对码农之家多多支持,你们的支持是我创作的动力!祝大家生活愉快! 

  • 相关阅读:
    在线流程图和思维导图开发技术详解(五)
    2022牛客蔚来杯补题(第九场)
    《Python程序设计案例教程》GUI连接数据库
    python实现长链矩阵的高效乘法
    【网络原理】UDP和TCP协议重点知识汇总
    Python爬虫-爬取全国各地市的邮编链接
    linux进程间通信之管道通信
    PerfView专题 (第九篇):洞察 C# 中的 LOH 内存碎片化
    4.k8s部署私人仓库并且部署java服务案例
    Java项目:JSP在线地下停车场车库管理系统
  • 原文地址:https://blog.csdn.net/wuxiaopengnihao1/article/details/127637725