std::basic_...iew::compare(3)

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

πŸŽ€ std::basic_...iew::compare(3) πŸŽ€

okay so this is the std::basic_...iew::compare(3) page from the C++ Standard Libary manual β€” yes, that's the spelling up top, and honestly iconic of it β€” and we're about to make string-view comparing make sense, bestie πŸ’–


πŸ’– NAME πŸ’–

std::basic_...iew::compare β€” std::basic_...iew::compare ✨

that's the name, that's the whole name, and yes it repeats itself. we love a consistent queen πŸ’…


✨ Synopsis ✨

six overloads, all constexpr, all (since C++17), and here's the exact synopsis so you can copy-paste it like the main character you are 🫢

          constexpr int compare( basic_string_view v ) const noexcept;
       (1) (since C++17)
          constexpr int compare( size_type pos1, size_type count1,
       (2) (since C++17)
                                 basic_string_view v ) const;
          constexpr int compare( size_type pos1, size_type count1,
          basic_string_view v,
       (3) (since C++17)
                                 size_type pos2, size_type count2 ) const;
          constexpr int compare( const CharT* s ) const;
       (4) (since C++17)
          constexpr int compare( size_type pos1, size_type count1,
       (5) (since C++17)
                                 const CharT* s ) const;
          constexpr int compare( size_type pos1, size_type count1,
       (6) (since C++17)
                                 const CharT* s, size_type count2 ) const;

Compares two character sequences. periodt 🌸

  1. The length rlen of the sequences to compare is the smaller of size() and v.size(). The function compares the two views by calling traits::compare(data(), v.data(), rlen), and returns a value according to the following table β€” but tables are not it, so here it is as bullets, bestie:
    • If Traits::compare(data(), v.data(), rlen) < 0: this is less than v, return < 0.
    • If Traits::compare(data(), v.data(), rlen) == 0 and size() < v.size(): this is less than v, return < 0.
    • If Traits::compare(data(), v.data(), rlen) == 0 and size() == v.size(): this is equal to v, return 0.
    • If Traits::compare(data(), v.data(), rlen) == 0 and size() > v.size(): this is greater than v, return > 0.
    • If Traits::compare(data(), v.data(), rlen) > 0: this is greater than v, return > 0.
  2. Equivalent to substr(pos1, count1).compare(v).
  3. Equivalent to substr(pos1, count1).compare(v.substr(pos2, count2)).
  4. Equivalent to compare(basic_string_view(s)).
  5. Equivalent to substr(pos1, count1).compare(basic_string_view(s)).
  6. Equivalent to substr(pos1, count1).compare(basic_string_view(s, count2)).

🌸 Parameters 🌸

  • v β€” view to compare
  • s β€” pointer to the character string to compare to
  • count1 β€” number of characters of this view to compare
  • pos1 β€” position of the first character in this view to compare
  • count2 β€” number of characters of the given view to compare
  • pos2 β€” position of the first character of the given view to compare

πŸ¦‹ Return value πŸ¦‹

Negative value if this view is less than the other character sequence, zero if the both character sequences are equal, positive value if this view is greater than the other character sequence. cute, clean, ordered β€” we love a predictable return type 😌


⭐ Complexity ⭐

1) Linear in the number of characters compared.


πŸ”₯ Example πŸ”₯

the page literally says // Run this code, so yeah β€” run it, bestie πŸ’…

        #include <string_view>

        int main()
        {
            using std::operator""sv;
            static_assert("abc"sv.compare("abcd"sv) < 0);
            static_assert("abcd"sv.compare("abc"sv) > 0);
            static_assert("abc"sv.compare("abc"sv) == 0);
            static_assert(""sv.compare(""sv) == 0);
        }

🌈 See also 🌈

  • compare β€” compares two strings β€” public member function of std::basic_string<CharT,Traits,Allocator>
  • operator==, operator!=, operator<, operator>, operator<=, operator>=, operator<=> β€” lexicographically compares two string views β€” (function template)
  • version tags stacked on the page, in order: (C++17), (removed in C++20), (removed in C++20), (removed in C++20), (removed in C++20), (removed in C++20), (C++20)

mwah πŸ’‹ this lil lesson came from http://cppreference.com, stamped 2024.06.10, and it's the std::basic_...iew::compare(3) page β€” bye bestie 🫢

πŸ“¦ where this page comes from