並べ替えられた配列で要素を二分して検索し、配列で最初に現れた位置を返します.


stlのlower_を参照できますboundアルゴリズム.
 
 
#include "stdafx.h"
#include 
using namespace std;


int  BinSearchFirst(int arr[], int begin, int end, int target)
{
	int mid = 0;
	int half = 0;
	int len = end - begin;
	while (len > 0)
	{
		half = len>>1;
		mid = begin + half;

		if (arr[mid] < target)
		{
			begin = mid + 1;
			len = len - half - 1;
		}
		else
			len = half;
	}
	return begin;
}

int main()
{
	int arr[] = {4, 10, 10, 30, 40, 100};

	int pos = BinSearchFirst(arr, 0, sizeof(arr) / sizeof(*arr), 10);
	cout<