std::basic_...nd_first_of(3)

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

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 carries noexcept(/* 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 in str.
  • (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 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. Protect your energy and your ranges. πŸ’€
  • (4) β€” Finds the first 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 first 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.

🌸 Parameters 🌸

  • str β€” string identifying characters to search for
  • pos β€” position at which to begin searching
  • 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. 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 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++11 and C++17 β€” behavior as published: noexcept for overloads (4,5) were accidentally dropped by LWG2064/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 of std::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