ranges::starts_with / ends_with

ranges::starts_with / ends_with (범위 접두·접미 검사)

한 범위가 다른 범위로 시작하는지(ends_with는 끝나는지) 검사하는 ranges 버전 알고리즘들이에요. <algorithm> 헤더, C++23부터.

출처: cppreference

본문

std::ranges::ends_with는 첫 범위가 두 번째 범위로 끝나는지 확인해요.

namespace std::ranges {
template< std::input_iterator I1, std::sentinel_for<I1> S1,
          std::input_iterator I2, std::sentinel_for<I2> S2,
          class Pred = ranges::equal_to, class Proj1 = std::identity,
          class Proj2 = std::identity >
constexpr bool ends_with( I1 first1, S1 last1, I2 first2, S2 last2,
                          Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {} );
}
  • [first2, last2)[first1, last1)의 접미사이면 true.
  • ranges::starts_with는 접두사인지 확인해요.
std::string s = "hello world";
bool e = std::ranges::ends_with(s, "world");   // true
bool st = std::ranges::starts_with(s, "hell"); // true

파일명 확장자나 문자열 접두/접미 매칭처럼 "무엇으로 시작/끝나는지"를 검사할 때 유용해요.

더 알아보기 (Learn more)

cppreference