728x90
728x170
*[STL] binary_search
- 이진 탐색 기능을 구현하고 있는 함수.
- 헤더 파일 : <algorithm>
- 템플릿
default (1) | template <class ForwardIterator, class T> bool binary_search (ForwardIterator first, ForwardIterator last, const T& val); |
default (2) | template <class ForwardIterator, class T, class Compare> bool binary_search (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); |
- 자세한 내용 : http://www.cplusplus.com/reference/algorithm/binary_search/?kw=binary_search
[예제 코드]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> #include <string> #include <algorithm> #include <cmath> #include <queue> #include <vector> #include <stdio.h> using namespace std; int main() { int n, m; scanf("%d", &n); vector<int> map(n); for (int i = 0; i < n; i++) scanf("%d", &map[i]); sort(map.begin(), map.end()); scanf("%d", &m); for (int i = 0; i < m; i++) { int tmp; scanf("%d", &tmp); printf("%d\n", binary_search(map.begin(), map.end(), tmp)); } } | cs |
[출력 결과]
5 4 1 5 2 3 5 1 3 7 9 5 1 1 0 0 1 |
728x90
그리드형(광고전용)
'Programming > C++' 카테고리의 다른 글
[header][container] queue (0) | 2017.11.08 |
---|---|
[header][container] map (0) | 2017.11.08 |
[header][container] array (C++11) (0) | 2017.11.08 |
[header][container] vector (0) | 2017.11.08 |
[STL] bitset (0) | 2017.10.26 |
[STL] sprintf (0) | 2017.10.21 |
[STL] getline (0) | 2017.10.20 |
[STL] ceil, floor, round (0) | 2017.09.23 |