관리 메뉴

Kim's Programming

Algorithm - upper_bound() 본문

STL - Algorithm/Algorithm - Binary Search

Algorithm - upper_bound()

Programmer. 2017. 6. 30. 00:47

원형)


1
2
3
4
5
6
7
8
9
//기본형
template <class ForwardIterator, class T>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                               const T& val);
 
//사용자 정의형
template <class ForwardIterator, class T, class Compare>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                               const T& val, Compare comp);
cs



의미)


[Iterator first, Iterator last) 사이에 있는 데이터들 중에서 val값 보다 큰 값중에서 첫 원소를 가리키는 Iterator를 리턴합니다. std::lower_bound()와는 다르게 같은 값을 가리키는 Iterator를 리턴하지는 않습니다.



소스)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <vector>
 
void Print(const std::vector<int>& target)
{
    for (std::vector<int>::const_iterator iterPos = target.cbegin(); iterPos != target.cend(); iterPos++)
        std::cout << *iterPos << " ";
    std::cout << std::endl;
}
 
void main()
{
    std::vector<int> vector = { 5,10,15,20,25,30 };
    std::vector<int>::iterator iter;
    iter = std::upper_bound(vector.begin(), vector.end(), 5);
 
    Print(vector);
    std::cout << *iter << std::endl;
}
 
cs



내용)


범위 내의 데이터들 중에서 상한 값을 가리키는 Iterator를 리턴합니다. 범위내에 val값보다 큰 값이 없다면 Iterator last를 리턴합니다.



결과)





'STL - Algorithm > Algorithm - Binary Search' 카테고리의 다른 글

Algorithm - binary_search()  (0) 2017.06.30
Algorithm - equal_range()  (0) 2017.06.30
Algorithm - lower_bound()  (0) 2017.06.30