std::ranges::contains, std::ranges::contains_subrange

cppreference.com

Example

ranges: namespace == std::ranges;

main: () = {
    haystack: std::array == (3, 1, 4, 1, 5);
    needle:   std::array == (1, 4, 1);
    bodkin:   std::array == (2, 5, 2);
 
    static_assert(
        ranges::contains(haystack, 4) &&
       !ranges::contains(haystack, 6) &&
        ranges::contains_subrange(haystack, needle) &&
       !ranges::contains_subrange(haystack, bodkin)
    );
 
    nums: std::array == (
        :std::complex<double> = (1, 2),
        :std::complex<double> = (3, 4),
        :std::complex<double> = (5, 6));
    static_assert(ranges::contains(nums, std::complex<double>(3, 4)));

    std::print("{} contains 4? {}\n",  haystack, ranges::contains(haystack, 4));
    std::print("{} contains {}? {}\n", haystack, needle, ranges::contains_subrange(haystack, needle));
    std::print("{} contains {}? {}\n", haystack, bodkin, ranges::contains_subrange(haystack, bodkin));
}

Output

[3, 1, 4, 1, 5] contains 4? true
[3, 1, 4, 1, 5] contains [1, 4, 1]? true
[3, 1, 4, 1, 5] contains [2, 5, 2]? false

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.