search the vector for the occurrence of a certain pattern with search_n : search_n « STL Algorithms Non modifying sequence operations « C++ Tutorial






#include <algorithm>
#include <vector>
#include <list>
#include <iostream>

using namespace std;

int main ()
{
    vector <int> v;

    for (int nNum = -9; nNum < 10; ++ nNum)
        v.push_back (nNum);

    v.push_back (9);
    v.push_back (9);

    list <int> l;

    for (int nNum = -4; nNum < 5; ++ nNum)
        l.push_back (nNum);

    // search the vector for the occurrence of pattern {9, 9, 9}
    vector <int>::iterator vl;
    vl = search_n ( v.begin () // Start range
                  , v.end ()   // End range
                  , 3          // Count of item to be searched for
                  , 9 );       // Item to search for

    if (vl != v.end ()){
        cout << "The sequence {9, 9, 9} found a match in the vector at ";
        cout << "offset-position: ";
        cout << distance (v.begin (), vl);

        cout << endl;
    }
    return 0;
}








25.14.search_n
25.14.1.Use search_n to find four consecutive elements with value 3
25.14.2.Use search_n with custom function to find four consecutive elements with value greater than 3
25.14.3.search the vector for the occurrence of a certain pattern with search_n