ranges::upper_bound

ranges::upper_bound (상한 경계 — ranges)

정렬된 범위에서 주어진 값보다 큰 첫 원소를 찾는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::upper_bound는 정렬된 범위에서 value보다 큰 첫 원소를 찾아요.

namespace std::ranges {
template< std::forward_iterator I, std::sentinel_for<I> S, class T,
          class Proj = std::identity,
          std::indirect_strict_weak_order<const T*, std::projected<I, Proj>>
              Comp = ranges::less >
constexpr I upper_bound( I first, S last, const T& value,
                         Comp comp = {}, Proj proj = {} );
}
  • 반환 값: value보다 큰 첫 원소 반복자. 없으면 last.
  • 복잡도: 로그적.
std::vector<int> v{1, 2, 2, 4, 5};
auto it = std::ranges::upper_bound(v, 2);
// 첫 4를 가리킴

lower_bound는 "크거나 같은" 첫 위치, upper_bound는 "보다 큰" 첫 위치예요. 둘의 차이로 같은 값의 구간을 계산할 수 있어요.

더 알아보기 (Learn more)

cppreference