- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Binary Search functions in C++ STL
Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the sorted array. The time complexity of binary search is O(1). This is a C++ program in which we implement various. Binary Search functions in C++ STL
Algorithm
Begin Initialize the vector of integer values. The functions are used here: binary_search(start_pointer, end_pointer, value) = Returns true if the value present in array otherwise false. lower_bound(start_pointer, end_pointer, value) = Returns pointer to “position of value” if container contains 1 occurrence of value. Returns pointer to “first position of value” if container contains multiple occurrence of value. Returns pointer to “position of next higher number than value” if container does not contain occurrence of value. upper_bound(start_pointer, end_pointer, value) = Returns pointer to “position of next higher valuethan value” if container contains 1 occurrence of value. Returns pointer to “first position of next higher number than last occurrence of value” if container contains multiple occurrence of value. Returns pointer to “position of next higher number than value” if container does not contain occurrence of value. Print the results. End.
Example Code
#include<bits/stdc++.h> using namespace std; int main() { vector<int> a = {6,7,10,14,16,20}; if (binary_search(a.begin(), a.end(), 50)) cout << "50 exists in vector"; else cout << "50 does not exist"; cout << endl; if (binary_search(a.begin(), a.end(), 7)) cout << "7 exists in the vector"; else cout << "7 does not exist"; cout << endl; cout << "The position of 7 using lower_bound "; cout << lower_bound(a.begin(), a.end(), 7) - a.begin(); cout << endl; cout << "The position of 7 using upper_bound "; cout << upper_bound(a.begin(), a.end(), 7) - a.begin(); cout << endl; }
Output
50 does not exist 7 exists in the vector The position of 7 using lower_bound 1 The position of 7 using upper_bound 2
- Related Articles
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Binary Search in C++ Standard Template Library (STL)
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Binary Search in C++
- Binary search in C#
- Binary Search in C++ program?
- Binary Search Tree - Search and Insertion Operations in C++
- Binary Search a String in C++
- Binary Search Tree Iterator in C++
- Unique Binary Search Trees in C++
- Recover Binary Search Tree in C++
- Explain Binary search in C language
- Binary Tree to Binary Search Tree Conversion in C++
- list cbegin() and cend() functions in C++ STL
- beta(), betaf() and betal() functions in C++ STL

Advertisements