C++ search()函数用法详解(深入了解,一文学会)

C++ search()函数用法详解(深入了解,一文学会)find end 函数用于在序列 A 中查找序列 B 最后一次出现的位置

大家好,欢迎来到IT知识分享网。

find_end() 函数用于在序列 A 中查找序列 B 最后一次出现的位置。那么,如果想知道序列 B 在序列 A 中第一次出现的位置,该如何实现呢?可以借助 search() 函数。

search() 函数定义在<algorithm>头文件中,其功能恰好和 find_end() 函数相反,用于在序列 A 中查找序列 B 第一次出现的位置。

C++ search()函数用法详解(深入了解,一文学会)

本文作者原创,转载请附上文章出处与本文链接。

C++ search()函数用法详解(深入了解,一文学会)目录

1 search()语法格式

2 search() 示例

2.1 第一种语法格式

2.2 第二种语法格式        第二种语法自定义规则,不做介绍因为感觉有点鸡肋。


1 search()语法格式

        和 find_end() 相同,search() 函数也提供有以下 2 种语法格式:

//查找 [first1, last1) 范围内第一个 [first2, last2) 子序列 ForwardIterator search (ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, ForwardIterator last2); //查找 [first1, last1) 范围内,和 [first2, last2) 序列满足 pred 规则的第一个子序列 ForwardIterator search (ForwardIterator first1, ForwardIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred);

其中,各个参数的含义分别为:

  • first1、last1:都为正向迭代器,其组合 [first1, last1) 用于指定查找范围(也就是上面例子中的序列 A);
  • first2、last2:都为正向迭代器,其组合 [first2, last2) 用于指定要查找的序列(也就是上面例子中的序列 B);
  • pred:用于自定义查找规则。该规则实际上是一个包含 2 个参数且返回值类型为 bool 的函数(第一个参数接收 [first1, last1) 范围内的元素,第二个参数接收 [first2, last2) 范围内的元素)。函数定义的形式可以是普通函数,也可以是函数对象。

实际上,第一种语法格式也可以看做是包含一个默认的 pred 参数,该参数指定的是一种相等规则,即在 [first1, last1) 范围内查找和 [first2, last2) 中各个元素对应相等的子序列;而借助第二种语法格式,我们可以自定义一个当前场景需要的匹配规则。

同时,search() 函数会返回一个正向迭代器,当函数查找成功时,该迭代器指向查找到的子序列中的第一个元素;反之,如果查找失败,则该迭代器的指向和 last1 迭代器相同。

2 search() 示例

#include <iostream> // std::cout #include <algorithm> // std::search #include <vector> // std::vector using namespace std; //以普通函数的形式定义一个匹配规则 bool mycomp1(int i, int j) { return (i % j == 0); } //以函数对象的形式定义一个匹配规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return (i % j == 0); } }; int main() { vector<int> myvector{ 1,2,3,4,8,12,18,12,18,1,2,3 }; int myarr[] = { 12,18,1 }; //调用第一种语法格式 vector<int>::iterator it = search(myvector.begin(), myvector.end(), myarr, myarr+3); if (it != myvector.end()) { cout << "第一个{12,18,1}的起始位置为:" << it - myvector.begin() << " ,*it = " << *it << endl; } int myarr2[] = { 18,1,2 }; //调用第二种语法格式 it = search(myvector.begin(), myvector.end(), myarr2, myarr2 + 3, mycomp2()); if (it != myvector.end()) { cout << "第一个{18,1,2}的起始位置为:" << it - myvector.begin() << " ,*it = " << *it; } return 0; }

C++ search()函数用法详解(深入了解,一文学会)

2.1 第一种语法格式

下面有两个示例:

示例1: 通过程序的执行结果可以看到,search() 函数找到了 myvector 容器中第一个 {1,2,6},起始位置下标为9,可以通过 myvector容器下标9、10、11来找到对应的1,2,6。

示例2:  通过程序的执行结果可以看到,search() 函数找到了 myvector 容器中第一个 {11,1,2},起始位置下标为8,可以通过 myvector容器下标8、9、10来找到对应的11,1,2。

示例3:如果未找到则不进行返回

#include <iostream> // std::cout #include <algorithm> // std::search #include <vector> // std::vector using namespace std; int main() { vector<int> myvector{ 1,2,3,4,8,12,18,12,18,1,2,6 }; int myarr[] = { 1,2,6 }; //调用第一种语法格式 vector<int>::iterator it = search(myvector.begin(), myvector.end(), myarr, myarr+3); if (it != myvector.end()) { cout << "第一个{1,2,6}的起始位置为:" << it - myvector.begin() << " ,*it = " << *it << endl; } return 0; }

C++ search()函数用法详解(深入了解,一文学会)

#include <iostream> // std::cout #include <algorithm> // std::search #include <vector> // std::vector using namespace std; int main() { vector<int> myvector{ 1,2,3,4,8,12,18,12,11,1,2,6 }; int myarr[] = { 11,1,2 }; //调用第一种语法格式 vector<int>::iterator it = search(myvector.begin(), myvector.end(), myarr, myarr+3); if (it != myvector.end()) { cout << "第一个{11,1,2}的起始位置为:" << it - myvector.begin() << " ,*it = " << *it << endl; } return 0; }

C++ search()函数用法详解(深入了解,一文学会)

 //不返回值 int myarr[] = { 1,2,100 }; //调用第一种语法格式 vector<int>::iterator it = search(myvector.begin(), myvector.end(), myarr, myarr + 3); if (it != myvector.end()) { cout << "第一个{11,1,2}的起始位置为:" << it - myvector.begin() << " ,*it = " << *it << endl; } 

2.2 第二种语法格式
        第二种语法自定义规则,不做介绍因为感觉有点鸡肋。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/142765.html

(0)
上一篇 2025-05-08 13:26
下一篇 2025-05-08 13:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信