Search String using binary_search() function in C++ STL Last Updated : 31 May, 2022 Comments Improve Suggest changes 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 Search String using binary_search() function in C++ STL A ajaymakvana Follow Improve Article Tags : Searching C++ Programs C++ DSA Binary Search +1 More Practice Tags : CPPBinary SearchSearching Similar Reads How to Read Binary Search Tree from File in C++? A binary search tree is a hierarchical data structure in which for every node in the tree, the value of all nodes in the left subtree is less than the node's value and the value of all nodes in the right subtree is greater than the node's value. This property of the binary search tree makes it effic 4 min read Count the number of 1's and 0's in a binary array using STL in C++ ? Given a binary array, the task is to count the number of 1's and 0's in this array using STL in C++. Examples: Input: arr[] = {1, 0, 0, 1, 0, 0, 1} Output: 1's = 3, 0's = 4 Input: arr[] = {1, 1, 1, 1, 0, 0, 1} Output: 1's = 5, 0's = 2 Approach: We can count the same using count_if() function present 1 min read Binary search in sorted vector of pairs How to apply STL binary_search to vector of pairs(key, value), given that vector is sorted by its first value(key) struct compare in the code contains two functions which compares the key(searching element) with the first element in the vector CPP /* C++ code to demonstrate how Binary Search can be 5 min read Binary Search Tree in C++ A Binary Search Tree (BST) is a type of binary tree in which the data is organized and stored in a sorted order. Unlike, a binary tree that doesn't follow a specific order for node placement, in a binary search tree all the elements on the left side of a node are smaller than the node itself, and el 10 min read How does generic find() function works in C++ STL? find(): The find() function is used to search the element in the given range and every STL container has the functionality to search the element using find() function. The generic find function works on every data type. Return Type: It returns an iterator to the first element in the range [first, la 3 min read Like