
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 in sorted vector of pairs in C++
In this article, we have a sorted vector of pairs. Our task is to search for a target key using binary search in the given vector of pairs.
Binary Search Algorithm
The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted.
In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or return the middle element.
What is Vector in C++?
A vector in C++ is a dynamic array that stores a collection of elements of the same data type, and it can be automatically resized. Here are some example scenarios to search for a key in a vector of pairs using binary search:
Scenario 1
Input: vector pair = {{7, 26}, {6, 76}, {4, 16}, {5, 36}, {11, 17}, {9, 19}}, target = 11 Output: 11 exist in vector
Scenario 2
Input: vector pair = {{5, 20}, {3, 14}, {9, 17}, {3, 31}}, target = 4 Output: 4 does not exist in vector
Steps for Binary search in sorted vector pairs
Following are the steps for implementing a binary search in the given sorted vector of pairs:
- Sort the given vector of pairs in ascending order using sort() function.
- Then we have used lower_bound() function on the whole vector using begin() and end() functions to search for the target key using compareByFirst function.
- The lower_bound() function points to the first key that is not less than the target value, and compareByFirst() function compares only the first value of the pair(key) with the target value.
- The compareByFirst() function returns true if the first value of vector is less than the key.
- Using an if-else condition, first we check if the iterator it is valid and then we check if the first value of the vector is equal to the target value.
- If the target is found in the vector pair, then 'target exists in vector' is returned.
Binary search in sorted vector of pairs in C++
Below is the code implementation of the steps mentioned above for binary search in sorted vector of pairs:
#include <algorithm> #include <iostream> #include <vector> using namespace std; // Comparison function for lower_bound bool compareByFirst(const pair<int, int> &p, int key) { return p.first < key; } int main() { vector<pair<int, int>> v = {{7, 26}, {6, 76}, {4, 16}, {5, 36}, {11, 17}, {9, 19}}; sort(v.begin(), v.end()); cout << "Sorted vector\n"; cout << "KEY\tVALUE\n"; for (auto &n : v) cout << n.first << '\t' << n.second << '\n'; int target = 11; auto it = lower_bound(v.begin(), v.end(), target, compareByFirst); if (it != v.end() && it->first == target) cout << target << " exists in vector\n"; else cout << target << " does not exist in vector\n"; return 0; }
The output of the above code is as follows:
Sorted vector KEY VALUE 4 16 5 36 6 76 7 26 9 19 11 17 11 exists in vector