Neerad
September 17th, 2002, 03:15 AM
Any body knows how is search template to be used which is declaed in algorithms??
|
Click to See Complete Forum and Search --> : How To Use Search (Template) in algorithms?? Neerad September 17th, 2002, 03:15 AM Any body knows how is search template to be used which is declaed in algorithms?? Philip Nicoletti September 17th, 2002, 07:52 AM 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.) #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; } Neerad September 17th, 2002, 10:08 PM 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 Graham September 18th, 2002, 04:17 AM A predicate is simply a function (or function object) that returns a bool. So, the following are predicates: 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. Neerad September 20th, 2002, 09:57 PM Thanx buddy, This methametics will some day kill me. I gotta start doing some maths again right from grade 5 onwards! Thanx a lot. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |