ranges::count

ranges::count (범위에서 개수 세기)

범위에서 특정 값 또는 조건을 만족하는 원소의 개수를 세는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::count는 범위에서 value와 같은 원소의 개수를, count_if는 술어를 만족하는 원소의 개수를 반환해요.

namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
          class T, class Proj = std::identity >
constexpr std::iter_difference_t<I>
    count( I first, S last, const T& value, Proj proj = {} );

template< std::input_iterator I, std::sentinel_for<I> S,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
constexpr std::iter_difference_t<I>
    count_if( I first, S last, Pred pred, Proj proj = {} );
}
  • ranges::count: value == *it(투영 후)인 원소 수.
  • ranges::count_if: pred(*it)(투영 후)이 참인 원소 수.
  • 반환 타입은 iter_difference_t<I>.
std::vector<int> v{1, 2, 1, 3, 1};
auto n = std::ranges::count(v, 1);   // 3

범위 하나로 값/조건의 빈도를 세는 간결한 함수예요.

더 알아보기 (Learn more)

cppreference