Results 1 to 6 of 6

Thread: Help to search the file in the C++ language using function

  1. #1
    Join Date
    Dec 2009
    Posts
    28

    Help to search the file in the C++ language using function

    Hello, Everyone. I am working in the MNC company as Software Developer. In my company C++ language can be used for creating the project. I have the knowledge of the functions of the C++ language. Still I can not know about the search(0 function of the C++ language. Therefore, I would like to know about the search() function of the C++ language. Also, I would like to know about use of the search() function in the coding of the C++ language. Thus, If you know about the search() function then reply me!!

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

    The search() function

    The search() function can be used to find out the sub-sequences in the given specified range. The following can be the syntax of the search() function in the C++ language as :
    Code:
    template <class FrwrdItrtr1, class FrwrdItrtr2>
       FrwrdItrtr1 search ( FrwrdItrtr1 fst1, FrwrdItrtr1 lst1,
                                 FrwrdItrtr2 fst2, FrwrdItrtr2 lst2 );

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    The search() function of C++

    The program listed below states you that how can you use the search() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    bool mprdct (int m, int n)
     {
      return (m==n);
    }
    int main () 
    {
      vector<int> mvctr;
      vector<int>::iterator i;
      for (int m=1; m<10; m++) mvctr.pushback(m*10);
      int mtch1[] = {40,50,60,70};
      i = search (mvctr.begin(), mvctr.end(), mtch1, mtch1+4);
      if (i!=mvctr.end())
        cout << "match1 found at position " << int(i-mvctr.begin()) << endl;
      else
        cout << "match1 not found" << endl;
      int mtch2[] = {20,30,50};
      i = search (mvctr.begin(), mvctr.end(), mtch2, mtch2+3, mprdct);
      if (i!=mvctr.end())
        cout << "match2 found at position " << int(i-mvctr.begin()) << endl;
      else
        cout << "match2 not found" << endl;
     return 0;
    }
    Output of the above code can be as :
    Code:
    match1 found at position 3
    match2 not found

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Use of the search()

    PROGRAM :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <numeric>
    template <class S>
    inline void PRINTELEMENTS (const S& cl, const char* optstr="")
    {
        typename S::const_iterator 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 j=fst; j<=lst; ++j) 
    {
            cl.insert(cl.end(),j);
        }
    }
    using namespace std;
    bool chkEvn (int elm, bool evn)
    {
        if (evn) 
    {
            return elm  2 == 0;
        }
        else 
    {
            return elm  2 == 1;
        }
    }
    int main()
    {
        vector<int> cl;
        INSERTELEMENTS(cl,1,9);
        PRINTELEMENTS(cl,"coll: ");
        bool chkEvnArgs[3] = { true, false, true };
        vector<int>::iterator ps;
        ps = search (cl.begin(), cl.end(),       
                      chkEvnArgs, chkEvnArgs+3, 
                      checkEvn);                     
        while (ps != cl.end()) 
    {
             cout << "subrange found starting with element "
                 << distance(cl.begin(),ps) + 1
                 << endl;
            ps = search (++ps, cl.end(),              
                          chkEvnArgs, chkEvnArgs+3, 
                          chkEvn);                     
        }
    }
    OUTPUT :
    Code:
    coll: 1 2 3 4 5 6 7 8 9
    subrange found starting with element 2
    subrange found starting with element 4
    subrange found starting with element 6
    Last edited by absolute55; 16-02-2010 at 05:04 PM.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    re: Help to search the file in the C++ language using function

    I suggest you to carefully go through the following parameters of the search(0 function of the C++ function :
    1. fst2, lst2 : These parameters can be used for the forward iterators to the final and starting position of sequence that can be searched for. The range used can be [fst2,lst2).
    2. fst1, lst1 : 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 [fst1,lst1).

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    The search() in C++

    This can be one more example on the search() function that helps you to learn more easily :
    Code:
          #include <algorithm>
          #include <list>
          #include <iostream>
         int main ()
          {
          #ifndef RWSTDNONAMESPACE
            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;
          }
    Program Output:
    Code:
          The subsequence, substring, was found at the 
          location identified by a '*'
               Here's a string with a *ubstring in it

Similar Threads

  1. Quick search Function in Opera with different Search engines?
    By Oorja in forum Technology & Internet
    Replies: 2
    Last Post: 23-02-2012, 05:53 PM
  2. What is the printf() function of the C language
    By Tailor in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 04:26 PM
  3. The floor() function in the C++ language
    By Ranjar in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 10:38 PM
  4. Replies: 5
    Last Post: 25-02-2010, 03:35 PM
  5. Search MAX / IF function in excel
    By Windowed in forum Windows Software
    Replies: 5
    Last Post: 05-01-2010, 02:00 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,751,807,127.50435 seconds with 16 queries