Defined in header
Call signature
template<std::input_iteratorI1,std::sentinel_for<I1>S1,std::input_iteratorI2,std::sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>constexprboolstarts_with(I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={}); (1) (since C++23)template<ranges::input_rangeR1,ranges::input_rangeR2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>constexprboolstarts_with(R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={}); (2) (since C++23)template</*execution-policy*/Ep,std::random_access_iteratorI1,std::sized_sentinel_for<I1>S1,std::random_access_iteratorI2,std::sized_sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>boolstarts_with(Ep&&policy,I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={}); (3) (since C++26)template</*execution-policy*/Ep,/*sized-random-access-range*/R1,/*sized-random-access-range*/R2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>boolstarts_with(Ep&&policy,R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={}); (4) (since C++26)For the definition of /*execution-policy*/, see
; for the definition of /*sized-random-access-range*/, see
.
Checks whether the target range matches the prefix of the source range. The elements (projected by proj1 and proj2 respectively) are compared using the binary predicate pred.
1) The source range is [first1, last1), and the target range is [first2, last2).
2) The source range is r1, and the target range is r2.
3,4) Same as (1,2), but executed according to policy.
The function-like entities described on this page are
(informally known as niebloids), that is:
Explicit template argument lists cannot be specified when calling any of them.
None of them are visible to
.
When any of them are found by
as the name to the left of the function-call operator,
is inhibited.
Parameters
first1, last1 - the iterator-sentinel pair defining the source
first2, last2 - the iterator-sentinel pair defining the target
r1 - the source range r2 - the target range pred - the predicate to be applied to the (projected) elements proj1 - the projection to be applied to the elements in the source range proj2 - the projection to be applied to the elements in the target range policy - the
to use Return value
true if the target range matches the prefix of the source range, false otherwise.
Complexity
Given
N1 as ranges::distance(first1,last1) or ranges::distance(r1), and
N2 as ranges::distance(first2,last2) or ranges::distance(r2):
1,2) At most min(N1,N2) applications of pred, proj1 and proj2.
3,4)𝓞(min(N1,N2)) applications of pred, proj1 and proj2.
Exceptions
3,4) During the execution process:
If the temporary memory resources required for parallelization are not available,
is thrown.
If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for
,
is invoked).
Notes
macroValueStdFeature
__cpp_lib_ranges_starts_ends_with
(C++23)std::ranges::starts_with, std::ranges::ends_withPossible implementation
structstarts_with_fn{template<std::input_iteratorI1,std::sentinel_for<I1>S1,std::input_iteratorI2,std::sentinel_for<I2>S2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<I1,I2,Pred,Proj1,Proj2>constexprbooloperator()(I1first1,S1last1,I2first2,S2last2,Predpred={},Proj1proj1={},Proj2proj2={})const{returnranges::mismatch(std::move(first1),last1,std::move(first2),last2,std::move(pred),std::move(proj1),std::move(proj2)).in2==last2;}template<ranges::input_rangeR>constexprautoget_end(R&&r){returnranges::end(r);}template<ranges::forward_rangeR>constexprautoget_end(R&&r){returnranges::next(ranges::begin(r),ranges::end(r));}template<ranges::input_rangeR1,ranges::input_rangeR2,classPred=ranges::equal_to,classProj1=std::identity,classProj2=std::identity>requiresstd::indirectly_comparable<ranges::iterator_t<R1>,ranges::iterator_t<R2>,Pred,Proj1,Proj2>constexprbooloperator()(R1&&r1,R2&&r2,Predpred={},Proj1proj1={},Proj2proj2={})const{return(*this)(ranges::begin(r1),get_end(r1),ranges::begin(r2),get_end(r2),std::move(pred),std::move(proj1),std::move(proj2));}};inlineconstexprstarts_with_fnstarts_with{};Example
Run this code
#include<algorithm>#include<iostream>#include<ranges>#include<string_view>intmain(){usingnamespacestd::literals;constexprautoascii_upper=[](char8_tc){returnu8'a'<=c&&c<=u8'z'?static_cast<char8_t>(c+u8'A'-u8'a'):c;};constexprautocmp_ignore_case=[=](char8_tx,char8_ty){returnascii_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';constexprstaticautov={1,3,5,7,9};constexprautoodd=[](intx){returnx%2;};static_assert(std::ranges::starts_with(v,std::views::iota(1)|std::views::filter(odd)|std::views::take(3)));}Output:
true false true false See also
(C++23)
checks whether a range ends with another range
(algorithm function object)
(C++20)
finds the first position where two ranges differ
(algorithm function object)
(C++20)
checks if the string starts with the given prefix
(public member function of std::basic_string<CharT,Traits,Allocator>)
(C++20)
checks if the string view starts with the given prefix
(public member function of std::basic_string_view<CharT,Traits>)