Namespaces
Variants

std::ranges::starts_with

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위에 대한 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
제약된 알고리즘
이 메뉴의 모든 이름은 네임스페이스에 속합니다std::ranges
비수정 시퀀스 연산
수정 시퀀스 연산
분할 연산
정렬 연산
이진 탐색 연산 (정렬된 범위에 대해)
       
       
집합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
       
       
순열 연산
폴드 연산
수치 연산
(C++23)            
초기화되지 않은 저장소에 대한 연산
반환 타입
 
헤더에 정의됨 <algorithm>
호출 시그니처
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 >
requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
constexpr bool
    starts_with( I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
                 Proj1 proj1 = {}, Proj2 proj2 = {} );
(1) (C++23부터)
template< ranges::input_range R1, ranges::input_range R2,
          class Pred = ranges::equal_to,
          class Proj1 = std::identity, class Proj2 = std::identity >
requires std::indirectly_comparable<ranges::iterator_t<R1>,
                                    ranges::iterator_t<R2>,
                                    Pred, Proj1, Proj2>
constexpr bool
    starts_with( R1&& r1, R2&& r2, Pred pred = {},
                 Proj1 proj1 = {}, Proj2 proj2 = {} );
(2) (C++23부터)

두 번째 범위가 첫 번째 범위의 접두사와 일치하는지 확인합니다.

1) N1과 N2가 각각 범위 [first1, last1)와 [first2, last2)의 크기를 나타낸다고 합시다. 만약 N1 < N2이면, false를 반환합니다. 그렇지 않으면, 범위 true의 모든 요소가 [first2, last2)의 대응 요소와 동일한 경우에만 [first1, first1 + N2)를 반환합니다. 비교는 이진 술어 pred를 두 범위의 요소에 적용하고 각각 proj1와 proj2에 의해 투영하여 수행됩니다.
2) (1)과 동일하지만, r1와 r2을 소스 범위로 사용하며, 마치 ranges::begin(r1)를 first1로, ranges:begin(r2)을 first2로, ranges::end(r1)를 last1으로, ranges::end(r2)을 last2로 사용하는 것과 같습니다.

이 페이지에 설명된 함수형 엔티티는 알고리즘 함수 객체 (비공식적으로 니블로이드라고 함)입니다. 즉:

  • 이들 중 하나를 호출할 때 명시적 템플릿 인자 목록을 지정할 수 없습니다.
  • 그 중 어느 것도 인자 종속 조회에 보이지 않습니다.
  • 그 중 하나가 일반 비한정 조회에 의해 함수 호출 연산자의 왼쪽 이름으로 발견되면, 인자 종속 조회가 억제됩니다.

매개변수

first1, last1 - 검사할 요소들의 범위 를 정의하는 반복자-센티널 쌍
r1 - 검사할 요소들의 범위
first2, last2 - 접두사로 사용될 요소들의 범위 를 정의하는 반복자-센티널 쌍
r2 - 접두사로 사용될 요소들의 범위
pred - 투영된 요소들을 비교하는 이항 조건자
proj1 - 검사할 범위의 요소들에 적용할 투영
proj2 - 접두사로 사용될 범위의 요소들에 적용할 투영

반환값

true 첫 번째 범위의 접두사와 두 번째 범위가 일치하는 경우, false 그렇지 않은 경우.

복잡도

선형: 최대 min ( N1, N2 ) 번의 술어(predicate) 및 두 프로젝션(projection) 적용.

가능한 구현

struct starts_with_fn
{
    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>
    requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
    constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2,
                              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return ranges::mismatch(std::move(first1), last1, std::move(first2), last2,
                                std::move(pred), std::move(proj1), std::move(proj2)
                               ).in2 == last2;
    }
    template<ranges::input_range R1, ranges::input_range R2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires std::indirectly_comparable<ranges::iterator_t<R1>,
                                        ranges::iterator_t<R2>,
                                        Pred, Proj1, Proj2>
    constexpr bool operator()(R1&& r1, R2&& r2,
                              Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2),
                       std::move(pred), std::move(proj1), std::move(proj2));
    }
};
inline constexpr starts_with_fn starts_with {};

참고 사항

기능 테스트 매크로 값 표준 기능
__cpp_lib_ranges_starts_ends_with 202106L (C++23) std::ranges::starts_with , std::ranges::ends_with

예제

#include <algorithm>
#include <iostream>
#include <ranges>
#include <string_view>
int main()
{
    using namespace std::literals;
    constexpr auto ascii_upper = [](char8_t c)
    {
        return u8'a' <= c && c <= u8'z' ? static_cast<char8_t>(c + u8'A' - u8'a') : c;
    };
    constexpr auto cmp_ignore_case = [=](char8_t x, char8_t y)
    {
        return ascii_upper(x) == ascii_upper(y);
    };
    static_assert(std::ranges::starts_with("const_cast", "const"sv));
    static_assert(std::ranges::starts_with("constexpr", "const"sv));
    static_assert(!std::ranges::starts_with("volatile", "const"sv));
    std::cout << std::boolalpha
              << std::ranges::starts_with(u8"Constantinopolis", u8"constant"sv,
                                          {}, ascii_upper, ascii_upper) << ' '
              << std::ranges::starts_with(u8"Istanbul", u8"constant"sv,
                                          {}, ascii_upper, ascii_upper) << ' '
              << std::ranges::starts_with(u8"Metropolis", u8"metro"sv,
                                          cmp_ignore_case) << ' '
              << std::ranges::starts_with(u8"Acropolis", u8"metro"sv,
                                          cmp_ignore_case) << '\n';
    constexpr static auto v = { 1, 3, 5, 7, 9 };
    constexpr auto odd = [](int x) { return x % 2; };
    static_assert(std::ranges::starts_with(v, std::views::iota(1)
                                            | std::views::filter(odd)
                                            | std::views::take(3)));
}

출력:

true false true false

참고 항목

범위가 다른 범위로 끝나는지 확인합니다
(알고리즘 함수 객체)
두 범위가 처음으로 달라지는 위치를 찾습니다
(알고리즘 함수 객체)
문자열이 주어진 접두사로 시작하는지 확인합니다
( std::basic_string<CharT,Traits,Allocator> 의 public 멤버 함수)
string view가 주어진 접두사로 시작하는지 확인합니다
( std::basic_string_view<CharT,Traits> 의 public 멤버 함수)