std::basic_...last_not_of(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::basic_string_view::find_last_not_of β€” the manual, but make it make sense

okay bestie, welcome to the std::basic_string_view::find_last_not_of page πŸ’– this is the C++ Standard Library manual, and today we're reading the std::basic_string_view::find_last_not_of entry together. yes the title is the same thing twice, that's just how these pages are, don't @ me 🫢


✨ SYNOPSIS ✨

four overloads, all since C++17 🌸 watch the const and noexcept placement, she's specific:

(1) constexpr size_type
        find_last_not_of( basic_string_view v, size_type pos = npos ) const noexcept;
(2) constexpr size_type
        find_last_not_of( CharT ch, size_type pos = npos ) const noexcept;
(3) constexpr size_type
        find_last_not_of( const CharT* s, size_type pos, size_type count ) const;
(4) constexpr size_type
        find_last_not_of( const CharT* s, size_type pos = npos ) const;

the whole vibe: finds the last character not equal to any of the characters in the given character sequence. and lowkey the search only ever considers the interval [0, pos] β€” nothing outside it, periodt πŸ’…

  • (1) finds the last character in this view that's not equal to any of the characters of v, starting at position pos.
  • (2) equivalent to find_last_not_of(basic_string_view(std::addressof(ch), 1), pos).
  • (3) equivalent to find_last_not_of(basic_string_view(s, count), pos).
  • (4) equivalent to find_last_not_of(basic_string_view(s), pos).

πŸŽ€ PARAMETERS πŸŽ€

  • v β€” the view to search for πŸ”
  • pos β€” position at which to start the search πŸ“
  • count β€” length of the string of characters to compare πŸ“
  • s β€” pointer to a string of characters to compare
  • ch β€” character to compare

🌟 RETURN VALUE 🌟

the position of the last character not equal to any of the characters in the given string β€” iconic, she found it ✨ or npos if no such character is found 😭 (that's the "nope, nothing, sorry bestie" answer).


πŸ¦‹ COMPLEXITY πŸ¦‹

O(size() * v.size()) at worst. yes, that's the worst case, ngl it can get expensive, plan accordingly πŸ’€


🌈 EXAMPLE 🌈

// Run this code

#include <string_view>
using std::operator""sv;

int main()
{
    static_assert(1 == "BCDEF"sv.find_last_not_of("DEF"));
                    //   ^
    static_assert(2 == "BCDEFG"sv.find_last_not_of("EFG", 3));
                    //    ^
    static_assert(2 == "ABBA"sv.find_last_not_of('A'));
                    //    ^
    static_assert(1 == "ABBA"sv.find_last_not_of('A', 1));
                    //   ^
}

the little ^ comments are doing the most and honestly? they ate πŸ“ they're pointing at exactly which character lands where. static_assert means this whole thing is checked at compile time, so if you break it, the compiler will let you know loudly πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

  • find β€” find characters in the view (public member function)
  • rfind β€” find the last occurrence of a substring (public member function)
  • find_first_of β€” find first occurrence of characters (public member function)
  • find_last_of β€” find last occurrence of characters (public member function)
  • find_first_not_of β€” find first absence of characters (public member function)
  • find_last_not_of β€” find last absence of characters (public member function of std::basic_string<CharT,Traits,Allocator>)
⚠️ heads up: that last one is a different find_last_not_of β€” it belongs to std::basic_string<CharT,Traits,Allocator>, not the string_view. same name, different family member, don't mix them up bestie 🫢

okay that's the whole page!! πŸ’œ source is cppreference.com, dated 2024.06.10, and this was the std::basic_...last_not_of(3) page from the C++ Standard Library manual. go write some static_asserts, you're gonna slay πŸŒΈβœ¨πŸŽ€

πŸ“¦ where this page comes from