C++如何使用std::search?代码示例

2021年3月19日18:51:37 发表评论 700 次浏览

std::search在头文件<algorithm>中定义了, 用于针对另一个序列找出满足条件(如果未定义这样的谓词, 则等于)的子序列的存在。

  • 它在序列[first1, last1)中搜索由[first2, last2)定义的子序列的第一个匹配项, 然后将一个迭代器返回到该匹配项的第一个元素, 如果没有找到匹配项, 则返回last1。
  • 它使用operator ==(版本1)或基于任何给定谓词(版本2)顺序比较两个范围内的元素。仅当[first2, last2)的所有元素都为true时, 才将[first1, last1)的子序列视为匹配。最后, std :: search返回第一个这样的事件。

可以在两个版本的任何一个中使用它, 如下所示:

为了使用==比较元素:

ForwardIterator1搜索(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); first1:将迭代器转发到要搜索的第一个容器的开头。 last1:将迭代器转发到要搜索的第一个容器的末尾。 first2:将迭代器转发到要搜索的第二个容器的子序列的开头。 last2:将迭代器转发到要搜索的第二个容器的子序列的末尾。返回:对[first1, last1)中第一个[first2, last2)的第一个元素的迭代器的迭代器;如果找不到任何匹配项, 则返回last1。

// C++ program to demonstrate the use of std::search
  
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
     int i, j;
  
     // Declaring the sequence to be searched into
     vector< int > v1 = { 1, 2, 3, 4, 5, 6, 7 };
  
     // Declaring the subsequence to be searched for
     vector< int > v2 = { 3, 4, 5 };
  
     // Declaring an iterator for storing the returning pointer
     vector< int >::iterator i1;
  
     // Using std::search and storing the result in
     // iterator i1
     i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end());
  
     // checking if iterator i1 contains end pointer of v1 or not
     if (i1 != v1.end()) {
         cout << "vector2 is present at index " << (i1 - v1.begin());
     } else {
         cout << "vector2 is not present in vector1" ;
     }
  
     return 0;
}

输出如下:

vector2 is present at index 2

为了基于谓词(或条件)进行比较:

ForwardIterator1搜索(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);所有参数都与以前的模板相同, 只是在pred上添加了一个参数:二进制函数, 接受两个元素作为参数(两个容器中的每个容器, 以相同的顺序), 并返回可转换为bool的值。返回的值指示在此函数的上下文中是否认为元素匹配。该函数不得修改其任何参数。这可以是一个函数指针或一个函数对象。返回:一个迭代器, 该迭代器在[first1, last1)中返回满足谓词的[first2, last2)的第一个匹配项的第一个元素, 如果找不到任何匹配项, 则返回last1。

// C++ program to demonstrate the use of std::search
// with binary predicate
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
  
// Defining the BinaryPredicate function
bool pred( int i, int j)
{
     if (i > j) {
         return 1;
     } else {
         return 0;
     }
}
  
int main()
{
     int i, j;
  
     // Declaring the sequence to be searched into
     vector< int > v1 = { 1, 2, 3, 4, 5, 6, 7 };
  
     // Declaring the subsequence to be compared to based
     // on predicate
     vector< int > v2 = { 3, 4, 5 };
  
     // Declaring an iterator for storing the returning pointer
     vector< int >::iterator i1;
  
     // Using std::search and storing the result in
     // iterator i1 based on predicate pred
     i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end(), pred);
  
     // checking if iterator i1 contains end pointer of v1 or not
     if (i1 != v1.end()) {
         cout << "vector1 elements are greater than vector2 starting "
              << "from position " << (i1 - v1.begin());
     } else {
         cout << "vector1 elements are not greater than vector2 "
              << "elements consecutively." ;
     }
  
     return 0;
}

输出如下:

vector1 elements are greater than vector2 starting from position 3

相关文章:

  • std :: search_n
  • std ::查找
  • std :: find_if, std :: find_if_not
  • std :: nth_element
  • std :: find_end

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: