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

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-03-2010
Member
 
Join Date: Dec 2009
Posts: 22
How to implement the binary_search() in C++

I am studying in the BSC-CS. I can not have the much more knowledge of the C++ language. I also can not know about the functions of the C++ language. So, I would like to know about the binary_search() of the C++ language. I also would like to know about how the binary_search() function can be implemented in the C++ programming. I hope anyone can has the answer for me and guides me about the binary_search() function.
Reply With Quote
  #2  
Old 03-03-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
The binary_search() in C++

The binary_search() function can be used in the C++ language to check that if value can be exists in the sorted array. The binary_search() function can returns the true value if an element can be in the range [fst,lst) can be similar to value and false otherwise. The following can be the syntax of the binary_search() function of the C++ language :
Code:
template <class FrwrdItrtr, class S>
  bool binary_search ( FrwrdItrtr fst, FrwrdItrtr lst,
                       const S& val );
Reply With Quote
  #3  
Old 03-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How to implement the binary_search() in C++

The following can be the parameters of the binary_search() function of the C++ language as :
1. val : This parameter of the binary_search() function can contains the value of an element to search for.
2. fst, lst : These parameter of the binary_search() function can contains forward iterators to the final and starting positions of the given specified sequence that can be searched. The range used can be [fst,lst).
Reply With Quote
  #4  
Old 03-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to implement the binary_search() in C++

I think you can have to go though the following code of lines that can describes you what is the use and how to implement the binary_search() function in the C++ programming :
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool mfunction (int i,int j) 
{
 return (i<j); 
}
int main ()
 {
  int mints[] = {1,2,3,4,5,4,3,2,1};
  wector<int> w(mints,mints+9);               
  sort (w.begin(), w.end());
  cout << "looking for a 3... ";
  if (binary_search (w.begin(), w.end(), 3))
    cout << "found!\n"; else cout << "not found.\n";
  sort (w.begin(), w.end(), mfunction);
  cout << "looking for a 6... ";
  if (binary_search (w.begin(), w.end(), 6, mfunction))
    cout << "found!\n"; else cout << "not found.\n";
  return 0;
}
Output:
Code:
looking for a 3... found!
looking for a 6... not found.
Reply With Quote
  #5  
Old 03-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
How to implement the binary_search()

The binary_search() function can searches from the beginning to an end for the value. The elements that can be in between beginning and end that can be searched could be in an order of ascending as can be defined by the "<" an operator. You can have noticed that a binary_search() function can not work unless an elements has bee searched can be in an order. If the value can be found, binary_search() function can returns true value else returns the false value. If the comparison variable can be specified then the binary_search() function can be used to compare an elements. The binary_search() function can runs in the logarithmic time.
Reply With Quote
  #6  
Old 03-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to implement the binary_search() in C++

PROGRAM :
Code:
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
bool mod_lesser ( int elm1, int elm2 )
{
   if (elm1 < 0)
      elm1 = - elm1;
   if (elm2 < 0)
      elm2 = - elm2;
   return elm1 < elm2;
}
int main( )
{
   using namespace std;
   list <int> L;
   list <int>::Itrator Itr;
   bool b1, b2;
   L.pshback( 50 );
   L.pshback( 10 );
   L.pshback( 30 );
   L.pshback( 20 );
   L.pshback( 25 );
   L.pshback( 5 );
   L.sort( );
   cout << "L = ( " ;
   for ( Itr = L.begin( ) ; Itr != L.end( ) ; Itr++ )
      cout << *Itr << " ";
   cout << ")" << endl;
   b1 = binary_search( L.begin( ), L.end( ), 10 );
   if  ( b1 )
      cout << "There is an element in list L with a value equal to 10."
           << endl;
   else
      cout << "There is no element in list L with a value equal to 10."
           << endl;
   L.sort ( greater<int> ( ) );
   b2 = binary_search( L.begin( ), L.end( ), 10 , greater<int> ( ) );
   if  ( b2 )
      cout << "There is an element in list L with a value equivalent to 10 "
           << "under greater than." << endl;
   else
      cout << "No element in list L with a value equivalent to 10 "
           << "under greater than." << endl;
   vector <int> K1;
   vector <int>::Itrator Itr1;
   int i;
   for ( i = -2 ; i <= 4 ; i++ )
   {
      K1.pshback( i );
   }
   sort ( K1.begin ( ) , K1.end ( ) , mod_lesser );
   cout << "Ordered under mod_lesser, vector K1 = ( " ;
   for ( Itr1 = K1.begin( ) ; Itr1 != K1.end( ) ; Itr1++ )
      cout << *Itr1 << " ";
   cout << ")" << endl;
   bool b3 = binary_search( K1.begin( ), K1.end( ), -3 , mod_lesser );
   if ( b3 )
      cout << "There is an element with a value equivalent to -3 "
           << "under mod_lesser." << endl;
   else
      cout << "There is not an element with a value equivalent to -3 "
           << "under mod_lesser." << endl;
}
Output
Code:
L = ( 5 10 20 25 30 50 )
There is an element in list L with a value equal to 10.
There is an element in list L with a value equivalent to 10 under greater than.
Ordered under mod_lesser, vector K1 = ( 0 -1 1 -2 2 3 4 )
There is an element with a value equivalent to -3 under mod_lesser.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to implement the binary_search() 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
Implement the copy_backward() in C++ Agustíne Software Development 5 16-02-2010 09:46 PM
Implement the equal_range() in C++ Garrick Software Development 5 15-02-2010 07:09 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 06:24 PM.