binary search in vector in c 2b 2b

Solutions on MaxInterview for binary search in vector in c 2b 2b by the best coders in the world

showing results for - "binary search in vector in c 2b 2b"
Daniele
08 Jul 2016
1#include <bits/stdc++.h>
2#include <iostream>
3#include <vector>
4#include <algorithm>
5
6using namespace std;
7//defining the comparator function returns true or false
8//not for binary search..
9bool f(int x, int y){
10	return x>y; //for decreasing order
11}
12	
13int main() {
14	
15	vector<int> A ={ 11,2,3,14 };
16	cout<<A[1]<<endl;//2
17  
18	sort(A.begin(), A.end()); // sort in order to perform binary search
19	cout<<A[1]<<endl;//3 after sorting
20	
21	bool present = binary_search(A.begin(), A.end(), 3);
22	cout<<present<<endl;//will return true
23  
24	present = binary_search(A.begin(), A.end(), 5);
25	cout<<present<<endl;//will return false
26  
27	A.push_back(100);//inserting new element from end
28  
29	present = binary_search(A.begin(), A.end(), 100);
30	cout<<present<<endl;
31  
32   	A.push_back(100);
33	A.push_back(100);
34	A.push_back(100);
35	A.push_back(121);
36	
37	//give me the iterator pointing to first element >= 100
38	vector<int>::iterator it = lower_bound(A.begin(), A.end(), 100);
39	//you can also use auto as c++ will see that a lower_bound is performed
40	//and it will figur it out that it is an iterator of vector<int>
41	//auto it = lower_bound(A.begin(), A.end(), 100);
42	//auto it2 = upper_bound(A.begin(), A.end(), 100);
43	
44	
45	
46	//give me an iterator pointing to first element >100
47	vector<int>::iterator it2 = upper_bound(A.begin(), A.end(), 100);
48	
49	//print the content of it and it2
50	cout<<*it<<" "<<*it2<<endl;
51	
52	//give me the number of hundreds(100)
53	cout<<it2-it<<endl; //4 it subtracts the indices 
54	
55	
56	//soritng the vector in reverse order
57	//use method overloading with sort by passing a comparator function 
58	//to control the ordering
59	sort(A.begin(), A.end(), f);
60	
61	//now print the sorted vector using iterator
62	vector<int>::iterator it3;
63	
64	for (it3 =A.begin(); it3!= A.end(); it3++){
65		cout<<*it3<<" ";
66	}
67	cout<<endl;
68	
69	//A shorter code for the above will be
70	for(int x: A){
71		//x++ wont change the vector content it will only print the changed one
72		cout<<x<<" ";
73	}
74	cout<<endl;
75	
76	//to change the vector content while iterating
77	//iterate it by referance by using &x
78	for(int &x: A){
79		x++;
80		cout<<x<<" ";
81	}
82	cout<<endl;
83  
84	return 0;
85}
queries leading to this page
lower index by binary search in c 2b 2bhow to do binary search on vectorstl binary search vectorbinary search in vector in cppbinary search vector stl c 2b 2bbinary search function c 2b 2blower bound vector binary searchbinary search vectorbinary search in c 2b 2b stlbinary search in vector stlbinary search stl on vectorbinary search with vector c 2b 2bbinary search in vector in c 2b 2bc 2b 2b binary search stlbinary search c 2b 2b stl vectorbinary search of characters in vectors in c 2b 2bbinary search vector struct c 2b 2bbinary search using vector c 2b 2bbinary search c 2b 2b vectorbinary search vector cpp stlreturn index binary search cpplower bound binary search c 2b 2bcan you use binary search on vectorc 2b 2b binary search vectorsearch function in stl will search forbin search vector cppbinary search on vectorsbs cpp stlinbuilt binary search in c 2b 2bbinary search in stl vectorvector binary searchbinary search function in bits 2fstdc 2b 2b hc 2b 2b binary search vector built in functionbinary search search vector c 2b 2b binary search in vector in c 2b 2bbinarysearch stlbinary search in vector stringbinary search in bits 2fstdc 2b 2b hbinary search an vector c 2b 2bbinary seach stldefault binary function in c 2b 2bbinary search function in vector c 2b 2bbinary search vector c 2b 2b implementationbinary search in vector c 2b 2b stlbinary search function in bits 2fstdc 2b 2bleftbound and right bound in cpp binary serach binary search in vector of maybeuninitbinary search c 2b 2b stlbinary search function vectorbinary search in vecotr stlbinary search on vectorbinary search in vectors c 2b 2bbinary search vector cppbinarysearch c 2b 2b stlbinary search in vectorc 2b 2b stl binary searchbinary search on vector cppbinary search vector c 2b 2b stlbinary search c 2b 2b stl upper boundwhat does binary search in c 2b 2b stl returndefault binary search function in c 2b 2bbinary search on vector in c 2b 2bbinary search function in stl c 2b 2bstd binary search with comparatorusing binary search for vectors in cppbinary search in vectors function c 2b 2bbinary search stl functionfind upper index using binary searchbinary search in vector array c 2b 2b binary search in vector c 2b 2bbinary search upper bound c 2b 2bbinary search in cpp stl in vectorbinary search in vector cppbinary search in sorted vector c 2b 2bc 2b 2b vector binary searchbinary search in array in stlbinary search stl vectorbinary search vector c 2b 2bbinary search in c 2b 2b vectorsorted search function c 2b 2bvetor binary searchlower bound c 2b 2b binary searchbinary search c 2b 2b stl on vectorshow to do binary search vector c 2b 2bc 2b 2b binary search on vectorc 2b 2b stl binary search vectorbinary search in vector in c 2b 2b