std::strcmp(3)

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

πŸ’… std::strcmp(3) β€” the C++ Standard Library page, but make it make sense

okay so bestie, this is std::strcmp from the C++ Standard Library, and honestly? she's the blueprint. let's get into it πŸŽ€


✨ NAME ✨

std::strcmp β€” yeah. that's it. that's the name. std::strcmp. iconic, no notes πŸ’…


🌸 SYNOPSIS 🌸

Defined in header <cstring>:

int strcmp( const char* lhs, const char* rhs );

So what does she do? She compares two null-terminated byte strings lexicographically. That's the whole gig πŸ¦‹

And here's the part you actually need to internalise: the sign of the result is the sign of the difference between the values of the first pair of characters that differ in the two strings β€” and both of those characters get interpreted as unsigned char. so like, it's not vibes-based, it's "first difference decides everything" energy ✨

⚠️ ok real talk for a sec: the behavior is undefined if lhs or rhs are not pointers to null-terminated strings. undefined. not "a little weird". not "she'll figure it out". undefined behavior, bestie 😭 do not test her.

🧚 PARAMETERS 🧚

  • lhs, rhs β€” pointers to the null-terminated byte strings you want to compare. that's them. two little string pointers, ready to be judged 🫢

πŸ‘‘ RETURN VALUE πŸ‘‘

  • Negative value β€” if lhs appears before rhs in lexicographical order. she comes first, periodt.
  • Zero β€” if lhs and rhs compare equal. twins. same energy. πŸ’–
  • Positive value β€” if lhs appears after rhs in lexicographical order. she's in her late-arrival era 🌈

πŸ”₯ EXAMPLE πŸ”₯

run this code πŸ‘‡

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

int main()
{
    std::vector<const char*> cats{"Heathcliff", "Snagglepuss", "Hobbes", "Garfield"};
    std::sort(cats.begin(), cats.end(), [](const char* strA, const char* strB)
    {
        return std::strcmp(strA, strB) < 0;
    });

    for (const char* cat : cats)
        std::cout << cat << '\n';
}

like... sorting cats. with std::strcmp. the range-based for at the end is chef's kiss πŸ˜­πŸ’œ

⭐ Output ⭐

Garfield
Heathcliff
Hobbes
Snagglepuss

alphabetical cat slay. Garfield said "I'm the main character" and honestly? she ate πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹

  • strncmp β€” compares a certain number of characters from two strings (function)
  • wcscmp β€” compares two wide strings (function)
  • memcmp β€” compares two buffers (function)
  • strcoll β€” compares two strings in accordance to the current locale (function)
  • C documentation for strcmp β€” the og. the source material. iykyk πŸ“š

okay that's the whole page, no cap, every flag-and-fact intact πŸ™ˆ lowkey one of the cleanest functions out there β€” just remember the unsigned char thing and the "must be null-terminated or it's undefined behavior" thing and you're golden πŸ§šβ€β™€οΈ

this gem is brought to you by http://cppreference.com, version 2024.06.10 β€” go drink some water and go touch a null terminator πŸ©΅πŸ“

πŸ“¦ where this page comes from