print_result:(haystack,needle)={pos:=std::distance(haystack.begin(),needle.begin());std::cout<<"In \"";forhaystackdo(c){std::cout<<c;}std::cout<<"\" found \"";forneedledo(c){std::cout<<c;}p:=posasstd::size_t;std::println("\" at position [{}..{})",p,p+needle.size());std::println("{}{}",std::string(4+p,' '),std::string(needle.size(),'^'));}main:()={usingstd::literals::_;secret:=="password password word..."sv;wanted:=="password"sv;found1:==std::ranges::find_end(secret.cbegin(),secret.cend(),wanted.cbegin(),wanted.cend());print_result(secret,found1);found2:==std::ranges::find_end(secret,"word"sv);print_result(secret,found2);found3:=std::ranges::find_end(secret,"ORD"sv,:(x:char,y:char)->bool={// uses a binary predicatereturnstd::tolower(x)==std::tolower(y);});print_result(secret,found3);found4:=std::ranges::find_end(secret,"SWORD"sv,std::ranges::equal_to(),std::identity(),// projects the 1st range:(c:char)std::tolower(c));// projects the 2nd rangeprint_result(secret,found4);static_assert(std::ranges::find_end(secret,"PASS"sv).empty());// => not found}
Output
In "password password word..." found "password" at position [9..17)
^^^^^^^^
In "password password word..." found "word" at position [18..22)
^^^^
In "password password word..." found "ord" at position [19..22)
^^^
In "password password word..." found "sword" at position [12..17)
^^^^^