std::wmemchr(3)

๐Ÿ“ฆ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

๐Ÿฆ‹ std::wmemchr(3) ๐Ÿฆ‹

okay so bestie, welcome to the C++ Standard Libary manual page for std::wmemchr โ€” yes, the manual name is literally โ€œC++ Standard Libaryโ€ and we are not fixing it, we respect the typo ๐Ÿ’…. This one is a wide-character search function, and it's lowkey iconic.


โœจ NAME โœจ

std::wmemchr - std::wmemchr

it's giving self-titled debut album โ€” the NAME field is just the function name twice, and honestly? slay.


๐ŸŽ€ Synopsis ๐ŸŽ€

Defined in header <cwchar> ๐Ÿ’–

const wchar_t* wmemchr( const wchar_t* ptr, wchar_t ch, std::size_t count ); (1)
wchar_t* wmemchr(       wchar_t* ptr, wchar_t ch, std::size_t count );       (2)

okay so what does it do? It locates the first occurrence of wide character ch in the initial count wide characters of the wide character array pointed to by ptr. fr fr, it's snooping through the first count wide chars and looking for your fave.

๐Ÿšจ caveat bestie: if count is zero, the function returns a null pointer. no searching, no vibes, just nullptr. periodt.

๐Ÿ’… Parameters ๐Ÿ’…

  • ptr โ€” pointer to the wide character array to be examined. the alley we're snooping through ๐Ÿ”
  • ch โ€” wide character to search for. the one we want ๐Ÿ’–
  • count โ€” number of wide characters to examine. how many slots we're checking, no more no less โœจ

๐Ÿซถ Return value ๐Ÿซถ

Pointer to the location of the wide character, or a null pointer if no such character is found. so if it finds ch, you get the address; if not, you get nullptr and a little moment of silence ๐Ÿ˜ญ


๐ŸŒˆ Example ๐ŸŒˆ

       // Run this code

        #include <clocale>
        #include <cwchar>
        #include <iostream>
        #include <locale>

        int main()
        {
            const wchar_t str[] =
       L"่ฏบไธ่ฝปไฟก๏ผŒๆ•…ไบบไธ่ดŸๆˆ‘\0่ฏบไธ่ฝป่ฎธ๏ผŒๆ•…ๆˆ‘ไธ่ดŸไบบใ€‚";
            wchar_t target = L'่ฎธ';
            const std::size_t sz = sizeof str / sizeof *str;
            if (const wchar_t* result = std::wmemchr(str, target, sz))
            {
                std::setlocale(LC_ALL, "en_US.utf8");
                std::wcout.imbue(std::locale("en_US.utf8"));
                std::wcout << "Found '" << target << "' at position " << result
       - str << '\n';
            }
        }

literally the code is doing the whole detective arc: it searches the wide string for L'่ฎธ', and if it finds it, it sets the locale and prints the position. ate and left no crumbs ๐Ÿ“


โญ Possible output โญ

Found '่ฎธ' at position 14

๐Ÿ‘‘ See also ๐Ÿ‘‘

  • memchr โ€” searches an array for the first occurrence of a character (function)
  • strchr โ€” finds the first occurrence of a character (function)
  • wcschr โ€” finds the first occurrence of a wide character in a wide string (function)
  • find
  • find_if โ€” finds the first element satisfying specific criteria
  • find_if_not โ€” (function template) โ€” (C++11)
  • C documentation for wmemchr

and that's the page, bestie ๐Ÿซถ sourced from the page URL http://cppreference.com, dated 2024.06.10, and filed under the std::wmemchr(3) page. go forth and search your wide chars with main character energy ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from