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 285 views
2 answers 315 views
3 answers 379 views
2 answers 315 views
2 answers 258 views
2 answers 307 views
3 answers 333 views
...