How to perform binary search on a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;

int main()
{
	std::vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 };
	bool found;

	found = std::binary_search(vec.begin(), vec.end(), 5);
	cout << found << endl;

	found = std::binary_search(vec.begin(), vec.end(), 348);
	cout << found << endl;

	return 0;
}

/*
run:

1
0

*/

 



answered Feb 22, 2018 by avibootz

Related questions

2 answers 200 views
2 answers 232 views
3 answers 245 views
2 answers 223 views
2 answers 181 views
2 answers 207 views
3 answers 207 views
...