Search String using binary_search() function in C++ STL Last Updated : 31 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The in-built STL library function binary_search() for searching whether a given string is present or not in the given string array. Binary search is a divide and conquers approach. The idea behind the binary search algorithm is to keep dividing the array in half until the element is found, or all the elements are exhausted. The middle item of the array is compared with the target value i.e. the value that needs to be searched, if it matches, it returns true otherwise if the middle term is greater than the target, the search is performed in the left sub-array. If the middle item is less than the target, the search is performed in the right sub-array. Example: Input: arr[] = {“Geeks”, “For”, “GeeksForGeek”} Search “Geeks” Output: String Founded in array Syntax: binary_search(starting_address, ending_address, value_of_string) Below is the implementation of the above approach: C++14 // C++ program to implement Binary // Search in Standard Template Library (STL) #include <algorithm> #include <iostream> using namespace std; void show_array(string arr[], int arraysize) { for (int i = 0; i < arraysize; i++) cout << arr[i] << ", "; } void binarySearch(string arr[], int size) { cout << "\nThe array is : \n"; show_array(arr, size); // Sort string array a for binary search as prerequisite sort(arr, arr + size); // Finding for "Geeks" cout << "\n\nSearching Result for \"Geeks\""; if (binary_search(arr, arr + size, "Geeks")) cout << "\nString Founded in array\n"; else cout << "\nString not Founded in array\n"; // Finding for string str string str = "Best"; cout << "\nSearching Result for \"Best\""; if (binary_search(arr, arr + size, str)) cout << "\nString Found in array"; else cout << "\nString not Found in array"; } // Driver code int main() { // Initialising string array a string arr[] = { "Geeks", "For", "GeeksForGeek" }; // Find size of array arr int size = sizeof(arr) / sizeof(arr[0]); // Function call binarySearch(arr, size); return 0; } Output: The array is : Geeks, For, GeeksForGeek, Searching Result for "Geeks" String Founded in array Searching Result for "Best" String not Found in array Time Complexity: O(N*log N), where N represents the number of elements present in the array Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find H-Index for sorted citations using Binary Search A ajaymakvana Follow Improve Article Tags : C++ Binary Search Practice Tags : CPPBinary Search Similar Reads Find a String in given Array of Strings using Binary Search Given a sorted array of Strings arr and a string x, The task is to find the index of x in the array using the Binary Search algorithm. If x is not present, return -1.Examples:Input: arr[] = {"contribute", "geeks", "ide", "practice"}, x = "ide"Output: 2Explanation: The String x is present at index 2. 6 min read Find H-Index for sorted citations using Binary Search Given an array citations[] consisting of N integers in non-increasing order, representing citations, the task is to find the H-index. H-Index is usually assigned to the researcher denoting the contributions made in terms of no of papers and citations. H-index(H) is the largest value such that the re 6 min read Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound) In C++, STL provide various functions like std::binary_search(), std::lower_bound(), and std::upper_bound() which uses the the binary search algorithm for different purposes. These function will only work on the sorted data.There are the 3 binary search function in C++ STL:Table of Contentbinary_sea 3 min read std::binary_search() in C++ STL In C++, STL provide std::binary_search() function which implements binary search algorithm to check whether an element exists in the given sorted range. It is defined inside <algorithm> header file. In this article, we will learn about std::binary_search() function in C++.Example:C++// C++ Pro 3 min read Linear Search vs Binary Search Prerequisites:Linear SearchBinary Search Important Differences Linear Search Binary SearchIn linear search input data need not to be in sorted.In binary search input data need to be in sorted order.It is also called sequential search.It is also called half-interval search.The time complexity of line 7 min read Is there any search faster than Binary Search? No, there is no search faster than Binary Search. Binary Search is the fastest searching algorithm for sorted data. It takes O(log2N) time to search any element in the sorted search space. In this article, we will discuss about how Binary Search works, it time complexity, comparison with other searc 3 min read Like