Namespaces
Variants

std::ranges:: starts_with

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <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 를 반환한다. 그렇지 않으면, 범위 [ first2 , last2 ) 의 모든 요소가 [ first1 , first1 + N2 ) 의 해당 요소와 동일할 때만 true 를 반환한다. 비교는 이진 조건자 pred 를 두 범위의 요소들에 각각 proj1 proj2 로 투영하여 적용하여 수행된다.
2) (1) 과 동일하지만, r1 r2 를 소스 범위로 사용하며, 마치 ranges:: begin ( r1 ) first1 으로, ranges:: begin ( r2 ) first2 로, ranges:: end ( r1 ) last1 으로, 그리고 ranges:: end ( r2 ) last2 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

  • 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
  • 이들 중 어느 것도 인수 의존 탐색 에 보이지 않습니다.
  • 이들 중 어느 것이 일반 비한정 탐색 에 의해 함수 호출 연산자 왼쪽의 이름으로 발견될 때, 인수 의존 탐색 이 억제됩니다.

목차

매개변수

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 멤버 함수)