C,C++

C++ lower_bound, upper_bound

콜라한캔 2018. 12. 26. 00:17

1. upper_bound


이진 탐색으로 key값을 탐색하고 [begin, end)에서 key보다 작지 않은 첫 번째 수의 iterator 반환


upper_bound(array, array + n, key);


2. lower_bound


이진 탐색으로 key값을 탐색하고 key값보다 큰 수를 찾아 iterator 반환


lower_bound(array, array + n, key);


<코드>

#include <iostream> 
#include <algorithm>

using namespace std;

int main()
{
	int arr[10] = {1,2,3,4,6,7,8,9,10,11};
	
	printf("lower_bound : %d\n", *(lower_bound(arr ,arr + 10, 4)));
	printf("upper_bound : %d\n", *(upper_bound(arr ,arr + 10, 4)));

	return 0;
}

<결과>