binary_search in vector in c++

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
//defining the comparator function returns true or false
//not for binary search..
bool f(int x, int y){
	return x>y; //for decreasing order
}
	
int main() {
	
	vector<int> A ={ 11,2,3,14 };
	cout<<A[1]<<endl;//2
  
	sort(A.begin(), A.end()); // sort in order to perform binary search
	cout<<A[1]<<endl;//3 after sorting
	
	bool present = binary_search(A.begin(), A.end(), 3);
	cout<<present<<endl;//will return true
  
	present = binary_search(A.begin(), A.end(), 5);
	cout<<present<<endl;//will return false
  
	A.push_back(100);//inserting new element from end
  
	present = binary_search(A.begin(), A.end(), 100);
	cout<<present<<endl;
  
   	A.push_back(100);
	A.push_back(100);
	A.push_back(100);
	A.push_back(121);
	
	//give me the iterator pointing to first element >= 100
	vector<int>::iterator it = lower_bound(A.begin(), A.end(), 100);
	//you can also use auto as c++ will see that a lower_bound is performed
	//and it will figur it out that it is an iterator of vector<int>
	//auto it = lower_bound(A.begin(), A.end(), 100);
	//auto it2 = upper_bound(A.begin(), A.end(), 100);
	
	
	
	//give me an iterator pointing to first element >100
	vector<int>::iterator it2 = upper_bound(A.begin(), A.end(), 100);
	
	//print the content of it and it2
	cout<<*it<<" "<<*it2<<endl;
	
	//give me the number of hundreds(100)
	cout<<it2-it<<endl; //4 it subtracts the indices 
	
	
	//soritng the vector in reverse order
	//use method overloading with sort by passing a comparator function 
	//to control the ordering
	sort(A.begin(), A.end(), f);
	
	//now print the sorted vector using iterator
	vector<int>::iterator it3;
	
	for (it3 =A.begin(); it3!= A.end(); it3++){
		cout<<*it3<<" ";
	}
	cout<<endl;
	
	//A shorter code for the above will be
	for(int x: A){
		//x++ wont change the vector content it will only print the changed one
		cout<<x<<" ";
	}
	cout<<endl;
	
	//to change the vector content while iterating
	//iterate it by referance by using &x
	for(int &x: A){
		x++;
		cout<<x<<" ";
	}
	cout<<endl;
  
	return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
binary search in vector in c++ vector binary search binary search with vector c++ binary search on vectors can you use binary search on vector how to do binary search vector c++ binary search vector stl c++ binary search in vector string binary search in vector c++ stl binary search stl on vector binary search in vector stl binary search vector cpp binary search function in vector c++ c++ binary search on vector c++ stl binary search vector binary search in stl vector bin search vector cpp how to do binary search on vector using binary search for vectors in cpp binary search in sorted vector c++ binary search c++ stl on vectors binary search search vector c++ binary search in cpp stl in vector binary search using vector c++ binary search in vector cpp binary search on vector cpp binary search c++ vector binary search function vector binary search on vector binary search in vector c++ binary search vector built in function c++ binary search vector binary search of characters in vectors in c++ lower bound vector binary search binary search in vectors c++ binary search upper bound c++ lower index by binary search in c++ lower_bound c++ binary search c++ vector binary search binary search in vector c++ vetor binary search return index binary search cpp c++ binary search stl binarysearch c++ stl binary search c++ stl vector leftbound and right bound in cpp binary serach binary search stl vector binary search c++ stl upper bound binary seach stl binary search in c++ vector binary search function c++ binary search an vector c++ binary search in c++ stl default binary search function in c++ binary search stl function what does binary search in c++ stl return binary search vector c++ inbuilt binary search in c++ sorted search function c++ binary search in vector of MaybeUninit binary search function in bits/stdc++ binary search vector binary search vector c++ stl std binary_search with comparator stl binary search vector default binary function in c++ binary search function in bits/stdc++.h binary search in array in stl bs cpp stl find upper index using binary search lower bound binary search c++ binary search function in stl c++ binarysearch stl binary search in bits/stdc++.h c++ stl binary search binary_search c++ stl Search function in STL will search for binary_search in vector in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source