std::basic_...find_last_of(3)
π
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:
- finds the last character equal to one of the characters in
str. - 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 π. - 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 usingTraits::length(s). if[s, s + Traits::length(s))is not a valid range, the behavior is undefined π. - finds the last character equal to
ch. - implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then finds the last character equal to one of the characters insv. this overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>istrueandstd::is_convertible_v<const StringViewLike&, const CharT*>isfalse.
in all cases, equality is checked by calling Traits::eq. π«Ά
π Parameters π
strβ string identifying characters to search forposβ position at which the search is to finishcountβ length of character string identifying characters to search forsβ pointer to a character string identifying characters to search forchβ character to search fortβ object (convertible tostd::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 toC++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 toC++98β behavior as published:there was no exception safety guaranteeβ correct behavior:added strong exception safety guarantee.LWG 2064β applied toC++11β behavior as published:overloads (3,4) were noexceptβ correct behavior:removed.LWG 2946β applied toC++17β behavior as published:overload (5) caused ambiguity in some casesβ correct behavior:avoided by making it a template.P1148R0β applied toC++17β behavior as published:noexcept for overloads (4,5) were accidentally dropped by LWG2064/LWG2946β correct behavior:restored. (the source table also has aC++11fragment 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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms