Namespaces
Variants

std::ranges:: ends_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:: forward_iterator < I1 > || std:: sized_sentinel_for < S1, I1 > ) &&
( std:: forward_iterator < I2 > || std:: sized_sentinel_for < S2, I2 > ) &&
std:: indirectly_comparable < I1, I2, Pred, Proj1, Proj2 >
constexpr bool ends_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 ( ranges:: forward_range < R1 > || ranges:: sized_range < R1 > ) &&
( ranges:: forward_range < R2 > || ranges:: sized_range < R2 > ) &&
std:: indirectly_comparable < ranges:: iterator_t < R1 > ,
ranges:: iterator_t < R2 > ,
Pred, Proj1, Proj2 >
constexpr bool ends_with ( R1 && r1, R2 && r2, Pred pred = { } ,

Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++23부터)

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

1) N1 ranges:: distance ( first1, last1 ) 로, N2 ranges:: distance ( first2, last2 ) 로 설정합니다:
  • 만약 N1 < N2 true 이면, false 를 반환합니다.
  • 그렇지 않으면, ranges:: equal ( std :: move ( first1 ) + ( N1 - N2 ) , last1,
    std :: move ( first2 ) , last2, pred, proj1, proj2 )
    를 반환합니다.
2) N1 ranges:: distance ( r1 ) 으로, N2 ranges:: distance ( r2 ) 으로 설정합니다.

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

목차

매개변수

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

반환값

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

복잡도

일반적으로 선형적: 최대 min(N1,N2) 번의 술어 및 두 프로젝션 적용. 술어와 두 프로젝션은 N1 < N2 true 인 경우 적용되지 않습니다.

만약 N1 N2 모두 상수 시간에 계산 가능하면 (즉, 두 iterator-sentinel 타입 쌍이 sized_sentinel_for 를 모델링하거나, 두 범위 타입이 sized_range 를 모델링하는 경우) 그리고 N1 < N2 true 인 경우, 시간 복잡도는 상수입니다.

가능한 구현

struct ends_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::forward_iterator<I1> || std::sized_sentinel_for<S1, I1>) &&
             (std::forward_iterator<I2> || std::sized_sentinel_for<S2, I2>) &&
             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
    {
        const auto n1 = ranges::distance(first1, last1);
        const auto n2 = ranges::distance(first2, last2);
        if (n1 < n2)
            return false;
        ranges::advance(first1, n1 - n2);
        return ranges::equal(std::move(first1), last1,
                             std::move(first2), last2,
                             pred, proj1, proj2);
    }
    template<ranges::input_range R1, ranges::input_range R2,
             class Pred = ranges::equal_to,
             class Proj1 = std::identity, class Proj2 = std::identity>
    requires (ranges::forward_range<R1> || ranges::sized_range<R1>) &&
             (ranges::forward_range<R2> || ranges::sized_range<R2>) &&
             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
    {
        const auto n1 = ranges::distance(r1);
        const auto n2 = ranges::distance(r2);
        if (n1 < n2)
            return false;
        return ranges::equal(views::drop(ranges::ref_view(r1),
                                         n1 - static_cast<decltype(n1)>(n2)),
                             r2, pred, proj1, proj2);
    }
};
inline constexpr ends_with_fn ends_with{};

참고 사항

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

예제

#include <algorithm>
#include <array>
static_assert
(
    ! std::ranges::ends_with("for", "cast") &&
    std::ranges::ends_with("dynamic_cast", "cast") &&
    ! std::ranges::ends_with("as_const", "cast") &&
    std::ranges::ends_with("bit_cast", "cast") &&
    ! std::ranges::ends_with("to_underlying", "cast") &&
    std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{3, 4}) &&
    ! std::ranges::ends_with(std::array{1, 2, 3, 4}, std::array{4, 5})
);
int main() {}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 4105 C++23 오버로드 ( 2 ) 가 크기 차이를
계산함 N1 - N2 [1]
다음으로 변경됨
N1 - static_cast < decltype ( N1 ) > ( N2 )
  1. 결과가 integer-class type 일 수 있으며, 이 경우 ranges::drop_view 를 생성할 수 없습니다.

참고 항목

범위가 다른 범위로 시작하는지 확인합니다
(알고리즘 함수 객체)
(C++20)
문자열이 주어진 접미사로 끝나는지 확인합니다
( std::basic_string<CharT,Traits,Allocator> 의 public 멤버 함수)
(C++20)
string view가 주어진 접미사로 끝나는지 확인합니다
( std::basic_string_view<CharT,Traits> 의 public 멤버 함수)