Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Implement the find_if() in C++

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-02-2010
Member
 
Join Date: Dec 2009
Posts: 22
Implement the find_if() in C++

Hello, Everybody. I had created the project in the C++ language. I can have the better knowledge of the C++ language. But I could not know anything about the functions of C++ language that can be defined in the Header files into the C++ library. So, I would like to know about the find_if() function. I also would like to know about how could I able to implement the find_if() function in the program of C++ language. Thus, If anyone can has the solution for my question then reply me!!

Last edited by Garett : 15-02-2010 at 08:51 PM.
Reply With Quote
  #2  
Old 15-02-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Implement the find_if() in C++

Hi, If the pred(*j) condition can be true Then the find_if() function can returns the first iterator j in the range of the [first, last) sequences. If there can be no such kind of iterators can be exist then the find_if() function can returns the last. The following can be the general syntax of the find_if() function :
Code:
template <class InptItrtr, class Predicate>
   InptItrtr find_if ( InptItrtr fst, InptItrtr lst, Predicate pred );

Last edited by Zecho : 15-02-2010 at 09:07 PM.
Reply With Quote
  #3  
Old 15-02-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Program : Implement the find_if()

Here is the code that can states you that the find_if() function how can work in the C++ language :
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool IsOd (int j)
 {
  return ((j%2)==1);
}
int main () 
{
  vector<int> mvctr;
  vector<int>::iterator i;
  mvctr.pushback(10);
  mvctr.pushback(25);
  mvctr.pushback(40);
  mvctr.pushback(55);
  i = find_if (mvctr.begin(), mvctr.end(), IsOd);
  cout << "The first odd value is " << *i << endl;
  return 0;
}
Output:
Code:
The first odd value is 25
Reply With Quote
  #4  
Old 15-02-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: Implement the find_if() in C++

PROGRAM :
Code:
      #include <vector>     
      #include <algorithm>  
      #include <functional> 
      #include <iostream>   
      int main ()
      {
           typedef std::vector<int, std::allocator<int> > Vector;
          const Vector::valuetype ar[] = { 0, 1, 2, 2, 3, 
                                             4, 2, 2, 6, 7 };
          const Vector w1 (ar, ar + sizeof ar / sizeof *ar);
          Vector::constiterator t = std::find (w1.begin (), 
                                                 w1.end (), 3);
          std::cout << *t << ' ';
          t = std::find_if (w1.begin (), w1.end (),
                   std::bind1st (std::equal_to<Vector::value_type>(), 
                   3));
          std::cout << *t << ' ';
          return 0;
      }
Program Output :
Code:
      3 3 2 2
Reply With Quote
  #5  
Old 15-02-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Implement the find_if() in C++

There can be one more example on the find_if() function that provides you the ides about how can you implement the it in the program :
Code:
#include <list>
#include <algorithm>
#include <iostream>
bool gretr10 ( int vl )
{
   return vl >10;
}
int main( )
{
   using namespace std;
   list <int> M;
   list <int>::iterator Itr;
   list <int>::iterator rslt;
   M.pushback( 5 );
   M.pushback( 10 );
   M.pushback( 15 );
   M.pushback( 20 );
   M.pushback( 10 );
   cout << "M = ( " ;
   for ( Itr = .begin( ) ; Itr != M.end( ) ; Itr++ )
      cout << *Itr << " ";
   cout << ")" << endl;
   rslt = find_if( M.begin( ), M.end( ), gretr10 );
   if ( rslt == M.end( ) )
      cout << "There is no element greater than 10 in list L."
           << endl;
   else
      rslt++;
      cout << "There is an element greater than 10 in list L,"
           << "\n and it is followed by a "
           <<  *(rslt) << "." << endl;
}
The Output of the above Code :
Code:
L = ( 5 10 15 20 10 )
There is an element greater than 10 in list L,
 and it is followed by a 20.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #6  
Old 15-02-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
The find_if() in C++

The find_if() function can be used to return the iterator to the first element in the given range as [fst,lst) to which you can apply prediction variable to the find_if() function, can be true. The behavior of the find_if() function template can be similar to the following syntax :
Code:
template<class InptItrtr, class Predicate>
  InptItrtr find_if ( InptItrtr first, InptItrtr last, Predicate pred )
  {
    for ( ; fst!=lst ; fst++ ) if ( pred(*fst) ) break;
    return fst;
  }
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Implement the find_if() in C++"
Thread Thread Starter Forum Replies Last Post
How to implement sum() faster in c#? Luis-Fernando Software Development 4 27-02-2010 09:25 PM
How can I implement the remove() in CPP Servant Software Development 5 22-02-2010 04:18 PM
CPP - Help to use the find_if() Garrick Software Development 5 12-02-2010 06:57 PM
Help me to implement adjacent_find() in C++ UseME Software Development 5 10-02-2010 10:26 PM
How to implement IP Sec Enriquee Technology & Internet 5 12-01-2010 06:28 AM


All times are GMT +5.5. The time now is 12:11 PM.