CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Unhappy How To Use Search (Template) in algorithms??

    Any body knows how is search template to be used which is declaed in algorithms??
    Spread Love And Knowledge

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Here is a simple example. Maybe if you give a brief
    description of your problem, more info can be provided.
    (might use find() instead of search(), etc.)

    Code:
    #include <iostream>
    #include <list>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    
    int main()
    {
        list<int> mylist;
        mylist.push_back(1);
        mylist.push_back(4);
        mylist.push_back(2);
        mylist.push_back(5);
        mylist.push_back(7);
    
        vector<int> myvector;
        myvector.push_back(2);
        myvector.push_back(5);
    
        //
        // check if "myvector" is in "mylist"
        //
    
        list<int>::iterator pos;
    
        pos = search(mylist.begin(),mylist.end(),myvector.begin(),myvector.end());
    
        //
        //
    
        if (pos != mylist.end())
        {
            cout << "myvector found in mylist starting with element ";
            cout << distance(mylist.begin(),pos) + 1 << endl; // maybe not use "+1" ?
        }
        else
        {
            cout << "myvector not found in mylist" << endl;
        }
    
    
        return 0;
    }

  3. #3
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Smile Thanx and more!

    Thanks a lot buddy,

    The problem was that i read the MSDN which explained in the following way

    search
    template<class FwdIt1, class FwdIt2>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
    FwdIt2 first2, FwdIt2 last2);
    template<class FwdIt1, class FwdIt2, class Pred>
    FwdIt1 search(FwdIt1 first1, FwdIt1 last1,
    FwdIt2 first2, FwdIt2 last2, Pred pr);
    The first template function determines the lowest value of N in the range [0, (last1 - first1) - (last2 - first2)) such that for each M in the range [0, last2 - first2), the predicate *(first1 + N + M) == *(first2 + M) is true. It then returns first1 + N. If no such value exists, the function returns last1. It evaluates the predicate (last2 - first2) * (last1 - first1) times, at most.

    The second template function behaves the same, except that the predicate is pr(*(first1 + N + M), *(first2 + M)).

    My problem was to comprehend the above language. I would still like to understand it (if someone help me out). The very first thing is 'predicate' which is not understood.

    Thanx buddy.
    Neerad
    Spread Love And Knowledge

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    A predicate is simply a function (or function object) that returns a bool. So, the following are predicates:
    Code:
    bool less(int i, int j)
    {
        return i < j;
    }
    
    class UnaryPredicate
    {
    public:
        UnaryPredicate(int val) : value_(val) {}
        bool operator()(int i)
        {
            return i == value_;
        }
    private:
        int value;
    };
    A binary predicate takes two arguments, and a unary predicate takes one argument. The STL defines lots of predicates for you: less<>, greater<>, not<> and so on.

    The MSDN descriptions of the algorithms are particularly awful. I would suggest that you invest in a good STL book (Josuttis or Austern are both worth reading) if you're going to be using STL in any great amount.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Smile Thanx

    Thanx buddy,

    This methametics will some day kill me. I gotta start doing some maths again right from grade 5 onwards!

    Thanx a lot.
    Spread Love And Knowledge

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured