std::equal

cppreference.com

Example

is_palindrome: (str: std::string_view) -> bool == {
    return std::equal(str.cbegin(), str.cbegin() + str.size() / 2, str.crbegin());
}

test: (str: std::string_view) = {
    text: std::string;
    if is_palindrome(str) {
        text = "is";
    }
    else {
        text = "is not";
    }
    std::cout << std::quoted(str)
              << std::format(" {} a palindrome", text)
              << "\n";
}

main: () = {
    test("radar");
    test("hello");

    static_assert(is_palindrome("civic"));
}

Output

"radar" is a palindrome
"hello" is not a palindrome

Back to top

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