std::strcmp(3)
π 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 iflhsorrhsare 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
lhsappears beforerhsin lexicographical order. she comes first, periodt. - Zero β if
lhsandrhscompare equal. twins. same energy. π - Positive value β if
lhsappears afterrhsin 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
- 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