Editing
Binary Search
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== C++ Built-in Binary Search == C++ provides built-in binary search algorithms in the Standard Template Library (STL) that can be used when direct implementation of the binary search algorithm is not necessary. The <algorithm> header provides three main binary search functions: lower_bound, upper_bound, and binary_search. === lower_bound === The lower_bound function returns an iterator pointing to the first element in the range that is not less than the given value. If the element is not found, it returns an iterator pointing to the first element greater than the given value. === upper_bound === The upper_bound function returns an iterator pointing to the first element in the range that is greater than the given value. === binary_search === The binary_search function returns a boolean indicating whether the given value is present in the range or not. It does not return the position of the element. Here's an example of using the built-in C++ binary search functions: <pre> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> nums = {1, 2, 2, 2, 3, 4, 5}; int target = 2; // Using lower_bound to find the first occurrence of the target auto lb = lower_bound(nums.begin(), nums.end(), target); if (lb != nums.end() && *lb == target) { cout << "First occurrence of target " << target << " is at index: " << lb - nums.begin() << endl; } else { cout << "Target " << target << " not found" << endl; } // Using upper_bound to find the position just after the last occurrence of the target auto ub = upper_bound(nums.begin(), nums.end(), target); cout << "Position after last occurrence of target " << target << " is at index: " << ub - nums.begin() << endl; // Using binary_search to check if the target is present in the range bool isPresent = binary_search(nums.begin(), nums.end(), target); cout << "Target " << target << (isPresent ? " is present" : " is not present") << " in the range" << endl; return 0; } </pre> [[Category:Binary Search]] [[Category:Algorithms]] [[Category:Search]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information