std::basic_...nd_first_of(3)
Page header vibes: std::basic_...nd_first_of(3) from the C++ Standard Library manual β section 3, bestie. The full glowing name of this function is std::basic_string::find_first_of. π
π NAME π
std::basic_string::find_first_of β yep, that's literally the name. It's the member function that hunts for the first character in your string matching any character from the character sequence you hand it. Periodt. β¨
β¨ Synopsis β¨
size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;
size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;
size_type find_first_of( const CharT* s, size_type pos = 0 ) const;
size_type find_first_of( CharT ch, size_type pos = 0 ) const;
template< class StringViewLike >
size_type find_first_of( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);
Overload tea β:
(1)β(noexcept since C++11),(constexpr since C++20)(2)β(constexpr since C++20)(3)β(constexpr since C++20)(4)β(noexcept since C++11),(constexpr since C++20)(5)β(since C++17),(constexpr since C++20), and the signature carriesnoexcept(/* see below */)
Okay so the main-character description: it finds the first character equal to one of the characters in the given character sequence. The search considers only the range [pos, size()). If none of the characters in the given character sequence is present in the range, npos will be returned. No cap. π¦
(1)β Finds the first character equal to one of the characters instr.(2)β Finds the first 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. That's UB, bestie, not a vibe.
(3)β Finds the first character equal to one of the characters in the character string pointed to bys. 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. Protect your energy and your ranges. π
(4)β Finds the first character equal toch.(5)β Implicitly convertstto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then finds the first 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.
πΈ Parameters πΈ
strβ string identifying characters to search forposβ position at which to begin searchingcountβ 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. Simple as that, iconically. π
π¨ Exceptions π¨
1,4)Throws nothing.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). That's the strong exception safety guarantee, slay. π₯
π§ Notes π§
Traits::eq() is used to perform the comparison. Lowkey important, highkey iconic. π
π» Example π»
// Run this code
#include <cassert>
#include <iostream>
#include <string>
#include <string_view>
int main()
{
using namespace std::literals;
std::string::size_type sz;
// (1)
sz = "alignas"s.find_first_of("klmn"s);
// ββββββββββββββββββββββββββ
assert(sz == 1);
sz = "alignof"s.find_first_of("wxyz"s);
// no match
assert(sz == std::string::npos);
// (2)
sz = "consteval"s.find_first_of("xyzabc", 0, 3);
// no match (Γ are not targets) ΓΓΓ
assert(sz == std::string::npos);
sz = "consteval"s.find_first_of("xyzabc", 0, 6);
// βββββββββββββββββββββββββββββββββ
assert(sz == 0);
// (3)
sz = "decltype"s.find_first_of("xyzabc");
// ββββββββββββββββββββββββββββββ
assert(sz == 2);
// (4)
sz = "co_await"s.find_first_of('a');
// ββββββββββββββββββββββββ
assert(sz == 3);
// (5)
sz = "constinit"s.find_first_of("int"sv);
// βββββββββββββββββββββββββββ
assert(sz == 2);
std::cout << "All tests passed.\n";
}
Output:
All tests passed.
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History lesson, but make it cunty. πβ¨
LWG 847β applied toC++98β behavior as published: there was no exception safety guarantee β correct behavior: added strong exception safety guaranteeLWG 2064β applied toC++11β behavior as published: overloads(3,4)werenoexceptβ correct behavior: removedLWG 2946β applied toC++17β behavior as published: overload(5)caused ambiguity in some cases β correct behavior: avoided by making it a templateP1148R0β applied toC++11andC++17β behavior as published:noexceptfor overloads(4,5)were accidentally dropped byLWG2064/LWG2946β correct behavior: restored
π 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_not_ofβ find first absence of characters (public member function)find_last_ofβ find last occurrence of characters (public member function)find_last_not_ofβ find last absence of characters (public member function)find_first_ofβ find first occurrence of characters (public member function ofstd::basic_string_view<CharT,Traits>)strspnβ returns the length of the maximum initial segment that consists of only the characters found in another byte string (function)
π Category π
* conditionally noexcept
Source/version and date, served fresh: http://cppreference.com β 2024.06.10. Stay iconic, bestie ππ¦π
π¦ 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