TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm binary_search() function

C++ algorithm binary_search() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm binary_search()

C++ Algorithm binary_search() function is used check whether the element in the range [first, last) is equivalent to val (or a binary predicate) and false otherwise.

  • The range [first, last) must satisfy all of the following conditions:
    • Partitioned with respect to element < val or comp (element, val).
    • Partitioned with respect to !(val < element) or !comp(value, element)
    • For all elements, if element < val or comp (element, val) is true then !(val < element) or !comp(val, element) is also true.
  • The first version uses operator< to compare the elements and the second version uses the given comparison function i.e. comp.

Syntax

default (1)       template <class ForwardIterator, class T>
                        bool binary_search (ForwardIterator first, ForwardIterator last,
                             const T& val);

custom (2)      template <class ForwardIterator, class T, class Compare>
                       bool binary_search (ForwardIterator first, ForwardIterator last,
                             const T& val, Compare comp);

Parameter

first: A forward iterator pointing to the first element in the range to be searched.

last: A forward iterator pointing to the past last element in the range to be searched.

comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.

val: A value of the upper bound to compare the elements in the range.

Return value

It returns true if an element equivalent to val is found otherwise, it returns false.

Complexity

On average, complexity is logarithmic in the distance between first and last: performs up to log2 (N) + 2 element comparisons Where N = last - first.

Data races

The object in the range [first, last) are accessed.

Exceptions

This function throws an exception if either an element comparison or an operation on iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of binary_search():

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {3, 1, 4, 6, 5};

  if (binary_search(v.begin(), v.end(), 4)) {
    cout << "4 found" << endl;
  }
  else {
    cout << "4 not found" << endl;
  }
  
  return 0;
}

Output:

4 found

Example 2

Let's see another simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::binary_search, std::sort
#include <vector>       // std::vector

using namespace std;

bool myfunction (int i,int j) { return (i<j); }

int main () {
  int myints[] = {1,2,3,4,5,4,3,2,1};
  vector<int> v(myints,myints+9);                         // 1 2 3 4 5 4 3 2 1

  // using default comparison:
  sort (v.begin(), v.end());

  cout << "looking for a 3... ";
  if (binary_search (v.begin(), v.end(), 3))
    cout << "found!\n"; else cout << "not found.\n";

  // using myfunction as comp:
  sort (v.begin(), v.end(), myfunction);

  cout << "looking for a 6... ";
  if (binary_search (v.begin(), v.end(), 6, myfunction))
    cout << "found!\n"; else std::cout << "not found.\n";

  return 0;
}

Output:

looking for a 3... found!
looking for a 6... not found.

Example 3

Let's see another simple example to compare the elements using comparison function:

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a[] = {1, 2, 3, 4, 5, 6, 7, 9, 10};
  vector<int> v(a, a+9);
  cout <<"\nHere are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  if (binary_search(v.begin(), v.end(), 3))
    cout <<"\nThe value 3 was found.";
  else
    cout <<"\nThe value 3 was not found.";
 
  if (binary_search(v.begin(), v.end(), 8))
    cout <<"\nThe value 8 was found.";
  else
    cout <<"\nThe value 8 was not found.";
 
  return 0;
}

Output:

Here are the values in the vector:
1 2 3 4 5 6 7 9 10 
The value 3 was found.
The value 8 was not found.

Example 4

Let's see another simple example:

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>  
#include <iomanip>     
using namespace std;
 
void print(vector <int> vs)
{   
    vector <int>::iterator i;
    for(i = vs.begin(); i != vs.end(); i++)
    {
        cout << left << setw(2) << *i;
    }
    cout << endl;
}
 
int main () {
    int arr[] = {1, 5, 2, 9, 8, 4, 3, 7, 6};
    int alen = sizeof(arr) / sizeof(int);
    vector <int> v(arr, arr + alen); 
 
    sort (v.begin(), v.end());
    cout << "Sorted vector elements : ";
    print(v);
    // Searching without using predicate
    cout << "Searching for 4 : ";
    if (binary_search (v.begin(), v.end(), 4))
        cout << "found!" << endl;
    else
        cout << "not found." << endl;
    // Searching using predicate
    cout << "Searching for element greater than 9 : ";
    if (binary_search(v.begin(), v.end(), 9, greater<int>()))
        cout << "found!" << endl;
    else
        cout << "not found." << endl;
    return 0;
}

Output:

Sorted vector elements : 1 2 3 4 5 6 7 8 9 
Searching for 4 : found!
Searching for element greater than 9 : not found.

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf