std::sub_match::compare(3)

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

πŸ’… std::sub_match::compare β€” okay bestie, let's get into it

So this is std::sub_match::compare from the C++ Standard Library, and honestly? It's a little niche, but it's ours now, so let's understand it together 🫢


✨ Synopsis ✨

int compare( const sub_match& m ) const;   (1) (since C++11)
int compare( const string_type& s ) const; (2) (since C++11)
int compare( const value_type* c ) const;  (3) (since C++11)

Three whole overloads, all since C++11, all const, all returning int. Iconic.

  1. Compares two sub_match directly, by comparing their underlying character sequences. Literally equivalent to str().compare(m.str()). That's it, that's the whole vibe.
  2. Compares a sub_match with a std::basic_string. Equivalent to str().compare(s).
  3. Compares a sub_match with a null-terminated sequence of the underlying character type, pointed to by s. Equivalent to str().compare(c).
Real talk though: this function is infrequently used directly by application code. Like, nobody's out here calling it in the group chat. Instead, one of the non-member comparison operators gets used. That's the main character. compare is just vibing backstage πŸ’€

πŸŽ€ Parameters πŸŽ€

  • m β€” a reference to another sub_match, i.e. the other one of these guys
  • s β€” a reference to a string you're comparing against
  • c β€” a pointer to a null-terminated character sequence of the underlying value_type, the thing you're comparing against

🌸 Return value 🌸

Here's the vibe check, spelled out in numbers:

  • less than zero if this sub_match is less than the other character sequence
  • zero if both underlying character sequences are equal (slay, a match πŸ’–)
  • greater than zero if this sub_match is greater than the other character sequence

So it's the classic three-way situation β€” negative, zero, positive. No cap.


πŸ¦‹ Example πŸ¦‹

This section is incomplete
Reason: no example

😭 not me finding out the example section is literally empty. Bestie... the docs said "no example" and I felt that. If you want, go write one β€” the page is begging for it πŸ™ˆ


🧚 See also 🧚

  • compare β€” compares two strings (public member function of std::basic_string<CharT,Traits,Allocator>)
  • str β€” converts to the underlying string type
  • operator string_type β€” converts to the underlying string type (public member function)
  • operator==, operator!=, operator<, operator<=, operator>, operator>=, operator<=> (removed in C++20) β€” compares a sub_match with another sub_match, a string, or a character (function template) β€” and note that <=> is the C++20 one πŸ’…

Also, just so nobody comes for me: this page is filed under the category Todo no example. Not a vibe, but we move.


Alright, that's the whole thing! This page came to you from cppreference.com, dated 2024.06.10 β€” big love to them for the source and the version, and go off, bestie, you now understand std::sub_match::compare πŸ’œπŸ”₯πŸ‘‘

πŸ“¦ where this page comes from