プログラミング道場 ACM/ICPC

2分検索

最終更新:

kit

- view
だれでも歓迎! 編集
/*
	template<class _FwdIt, class _Ty>
	bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)

	template<class _FwdIt, class _Ty, class _Pr>
	bool binary_search(_FwdIt _First, _FwdIt _Last,	const _Ty& _Val, _Pr _Pred)
*/

#include <algorithm>
#include <iostream>
#include <functional>
 
using namespace std;

int main()
{
	//ソートされている必要がある
	int a[] = {1,2,4,7,9};

	//true
	cout << ( binary_search(a,a+5,4) ? "true" : "false" ) << endl;

	//false
	cout << ( binary_search(a,a+5,3) ? "true" : "false" ) << endl;

	sort(a, a+5, greater<int>());

	//false 第3引数でソートされた配列を2分検索する
	cout << ( binary_search(a,a+5,3,greater<int>()) ? "true" : "false" ) << endl;

}
人気記事ランキング
ウィキ募集バナー