std::basic_...find_last_of(3)

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

πŸ’… okay bestie, it's std::basic_...find_last_of πŸ’…

so this page is from the C++ Standard Libary manual (yes, that's how the manual name is spelled in the source, iykyk), and it's all about std::basic_string::find_last_of β€” the member function that finds the last character matching any character in a set you give it. no cap, it's lowkey iconic for path splitting. πŸ¦‹

πŸŽ€ NAME πŸŽ€

std::basic_string::find_last_of β€” std::basic_string::find_last_of

🌸 Synopsis 🌸

here's the synopsis exactly as the manual lays it out (the line wraps are doing the absolute most 😭):

          size_type find_last_of( const basic_string& str,         (1)
       (noexcept since C++11)
                                  size_type pos = npos ) const;
       (constexpr since C++20)
          size_type find_last_of( const CharT* s,
                                  size_type pos, size_type count ) (2)
       (constexpr since C++20)
          const;
          size_type find_last_of( const CharT* s, size_type pos =  (3)
       (constexpr since C++20)
          npos ) const;
          size_type find_last_of( CharT ch, size_type pos = npos ) (4)
       (noexcept since C++11)
          const;
       (constexpr since C++20)
          template< class StringViewLike >

          size_type                                                    (since
       C++17)
              find_last_of( const StringViewLike& t,               (5)
       (constexpr since C++20)

                            size_type pos = npos ) const
          noexcept(/* see below */);

πŸ’– Description πŸ’–

okay so the whole vibe is: it finds the last character equal to one of the characters in the character sequence you hand it. πŸ’…

the exact search algorithm is not specified. the search considers only the range [0, pos]. if none of the characters in the given character sequence is present in that range, npos gets returned. periodt.

here's what each overload is serving:

  1. finds the last character equal to one of the characters in str.
  2. finds the last character equal to one of the characters in the range [s, s + count). this range can include null characters. if [s, s + count) is not a valid range, the behavior is undefined πŸ’€.
  3. finds the last character equal to one of the characters in the character string pointed to by s. the length of the string is determined by the first null character using Traits::length(s). if [s, s + Traits::length(s)) is not a valid range, the behavior is undefined 😭.
  4. finds the last character equal to ch.
  5. implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then finds the last character equal to one of the characters in sv. this overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

in all cases, equality is checked by calling Traits::eq. 🫢

πŸŽ€ Parameters πŸŽ€

  • str β€” string identifying characters to search for
  • pos β€” position at which the search is to finish
  • count β€” length of character string identifying characters to search for
  • s β€” pointer to a character string identifying characters to search for
  • ch β€” character to search for
  • t β€” object (convertible to std::basic_string_view) identifying characters to search for

πŸ¦‹ Return value πŸ¦‹

position of the found character or npos if no such character is found. that's it. simple, clean, iconic. ✨

πŸ’… Exceptions πŸ’…

  • 1,4) throws nothing. (slay, no drama.)
  • 5) noexcept specification:
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

if an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). πŸ’–

🌟 Example 🌟

// Run this code

#include <iostream>
#include <string>

int main()
{
    const std::string path = "/root/config";
    auto const pos = path.find_last_of('/');
    const auto leaf = path.substr(pos + 1);

    std::cout << leaf << '\n';
}

πŸ–₯️ Output πŸ–₯️

config

πŸ”₯ Defect reports πŸ”₯

the following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, the committee said "we'll fix it in post" and honestly? ate.)

  • LWG 141 β€” applied to C++98 β€” behavior as published: overload (1) could only return npos if pos >= size() β€” correct behavior: the search range is [0, size()) in this case.
  • LWG 847 β€” applied to C++98 β€” behavior as published: there was no exception safety guarantee β€” correct behavior: added strong exception safety guarantee.
  • LWG 2064 β€” applied to C++11 β€” behavior as published: overloads (3,4) were noexcept β€” correct behavior: removed.
  • LWG 2946 β€” applied to C++17 β€” behavior as published: overload (5) caused ambiguity in some cases β€” correct behavior: avoided by making it a template.
  • P1148R0 β€” applied to C++17 β€” behavior as published: noexcept for overloads (4,5) were accidentally dropped by LWG2064/LWG2946 β€” correct behavior: restored. (the source table also has a C++11 fragment right above this row; keeping it here so no detail gets lost 🫢)

πŸ‘‘ See also πŸ‘‘

  • find β€” finds the first occurrence of the given substring (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_first_not_of β€” find first absence of characters (public member function)
  • find_last_not_of β€” find last absence of characters (public member function)
  • find_last_of β€” find last occurrence of characters (public member function of std::basic_string_view<CharT,Traits>)

🌈 Category 🌈

* conditionally noexcept

mwah πŸ’‹ β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::basic_...find_last_of(3). stay hydrated and may your find_last_of('/') always return the right slash. πŸ“

πŸ“¦ where this page comes from