Results 1 to 6 of 6

Thread: Use of the search_n() in CPP

  1. #1
    Join Date
    Dec 2009
    Posts
    26

    Use of the search_n() in CPP

    Hello, I am studying in the PQR college. I had just started to learn C++ language from the institute. I am interested in the programming and I had heard that the C++ language can be the very simple programming language. So, I would like to know about the search_n() function of the C++ language. Also, I would like to know about the use of the search_n() function in the C++ programming. Can anyone has the solution for me on the search_n() function. Reply Me!!!
    Last edited by Garrick; 16-02-2010 at 05:42 PM.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    The search_n()

    Hi, To Search the given specified range [fst,lst) for a succession of count an elements, the search_n() function can be used in the C++ language. The following can be the general syntax of the search_n() function :
    Code:
    template <class FrwrdItrtr, class Sz, class S>
       FrwrdItrtr search_n ( FrwrdItrtr fst, FrwrdItrtr lst,
                                  Sz cnt, const S& value );

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Use of the search_n()

    The following listed parameters of the search_n() function can help's you to use the search_n() function properly :
    1. value : This parameter can be used to compare the Individual value or it can be used as an argument for predicate.
    2. fst, lst : These parameters can be used for the forward iterators to the final and starting positions of the sequence that can be searched. The range used can be as [first,last).
    3. cnt : This parameter can be used for the Minimum number of successive an elements that can be meeting the predicate that can be considered as match.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Use of the search_n() in CPP

    Program :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    bool mypredicate (int p, int q) 
    {
      return (p==q);
    }
    int main ()
     {
      int mints[]={10,20,30,30,20,10,10,20};
      vector<int> mvctr (mints,mints+8);
      vector<int>::iterator i;
      i = search_n (mvctr.begin(), mvctr.end(), 2, 30);
      if (i!=mvctr.end())
        cout << "two 30s found at position " << int(i-mvctr.begin()) << endl;
      else
        cout << "match not found" << endl;
      i = search_n (mvctr.begin(), mvctr.end(), 2, 10, mprdct);
      if (i!=mvctr.end())
        cout << "two 10s found at position " << int(i-mvctr.begin()) << endl;
      else
        cout << "match not found" << endl;
        return 0;
    }
    Output :
    Code:
    Two 30s found at position 2
    Two 10s found at position 5

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    The search_n() in CPP

    The following code of lines can help you to learn the search_n() function more precisely :
    Code:
          #include <algorithm>
          #include <list>
          #include <iostream>
          int main ()
          {
          #ifndef _RWSTD_NO_NAMESPACE
            using namespace std;
          #endif
            char sq[40]    = "Here's a string with a substring in it";
            char sbsq[10] = "substring";
            list<char,allocator<char> > sequence(sq, sq+38);
            list<char,allocator<char> > subseqnc(sbsq, sbsq+9);
            cout << endl << "The subsequence, " << sbsq 
                 << ", was found at the ";
            cout << endl << "location identified by a '*'" 
                 << endl << "     ";
            list<char,allocator<char> >::iterator plc;
            plc = search(squnc.begin(), squnc.end(),
                           sbsqnc.begin(), sbsqnc.end());
            *plc = '*';
            for (list<char,allocator<char> >::iterator j = 
                 squnc.begin(); j != squnc.end(); j++)
              cout << *j;
            cout << endl;
           return 0;
          }
    The Output of the above Program :
    Code:
          The subsequence, substring, was found at the 
          location identified by a '*'
               Here's a string with a *ubstring in it

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Use of the search_n() in CPP

    Program :
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <functional>
    #include <numeric>
    template <class S>
    inline void PRINTELEMENTS (const S& cl, const char* optstr="")
    {
        typename S::constiterator ps;
        std::cout << optstr;
        for (ps=cl.begin(); ps!=cl.end(); ++ps) 
    {
            std::cout << *ps << ' ';
        }
        std::cout << std::endl;
    }
    template <class S>
    inline void INSERTELEMENTS (S& cl, int fst, int lst)
    {
        for (int a=fst; a<=lst; ++a) 
    {
            cl.insert(cl.end(),a);
        }
    }
    using namespace std;
    int main()
    {
       deque<int> cl;
       INSERTELEMENTS(cl,1,9);
       PRINTELEMENTS(cl);
       deque<int>::iterator ps;
       ps = search_n (cl.begin(), cl.end(), 4, 3);                    
       if (ps != cl.end()) 
    {
           cout << "four consecutive elements with value 3 "
                << "start with " << distance(cl.begin(),ps) +1
                << ". element" << endl;
       }
       else 
    {
           cout << "no four consecutive elements with value 3 found"
                << endl;
       }
    }
    Output :
    Code:
    1 2 3 4 5 6 7 8 9
    no four consecutive elements with value 3 found
    Last edited by kelfro; 16-02-2010 at 06:36 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,138,342.01470 seconds with 16 queries