📜  在C++中搜索std ::

📅  最后修改于: 2021-05-30 05:56:41             🧑  作者: Mango

std :: search在头文件中定义,用于查找满足另一个序列的条件(如果未定义此类谓词,则等于)的子序列的存在。

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

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

  1. 为了使用==比较元素:
    // C++ program to demonstrate the use of std::search
      
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        int i, j;
      
        // Declaring the sequence to be searched into
        vector v1 = { 1, 2, 3, 4, 5, 6, 7 };
      
        // Declaring the subsequence to be searched for
        vector v2 = { 3, 4, 5 };
      
        // Declaring an iterator for storing the returning pointer
        vector::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
    
  2. 为了基于谓词(或条件)进行比较:
    // C++ program to demonstrate the use of std::search
    // with binary predicate
    #include 
    #include 
    #include 
    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 v1 = { 1, 2, 3, 4, 5, 6, 7 };
      
        // Declaring the subsequence to be compared to based
        // on predicate
        vector v2 = { 3, 4, 5 };
      
        // Declaring an iterator for storing the returning pointer
        vector::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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”